5 Replies - 331 Views - Last Post: 17 December 2013 - 07:08 PM Rate Topic: -----

#1 JNoobie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-December 13

Replacing one output with another using counter and if

Posted 15 December 2013 - 04:31 PM

Hi. First of. Im a newbie. Just started to try to learn java with this page http://math.hws.edu/javanotes/
p
Ive read untill half of chapter 3. So I learned somewhat about while, and loops and if and else. also previous chapters were about boolean expressions and operands. I thought I wanted to make a project of my own, with the stuff I learned sofar. So, what about a multiplication program?

Here is the working version :


OBSERVE: TextIO.putln is a class used to teach java in the above mentioned tutorial and equals
System.out.println
and TextIO.getInt is the equivalent of original system way of getting a Int number from user.



public class Multi {

	public static void main(String[]args) {
		
		int numb1;
		int numb2;
		int answ;
		int result;
		char com;
		char inp;
		
		com ='a';
		
		TextIO.putln("Hello. Im a small program that will teach you multiplication." + 
                " Every time you type \"a\" you are going to get a new set of numbers to multiply." + 
                " You simply type in the result and hit enter. Go ahead and try by typing \"a\".");
		
		inp = TextIO.getlnChar();
		
		
		while (inp == com) {                              // problem is within this whileloop. How to exchange 
			numb1 = (int) (13 * Math.random());       // the usual "CORRECT" message with some other
			numb2 = (int) (13 * Math.random());       // variations.
			result = numb1 * numb2;
			TextIO.put( numb1 + " * " + numb2 + " = ");
			answ = TextIO.getlnInt();
			TextIO.putln();
			if (answ == result) {
				TextIO.putln("CORRECT!! type \"a\" to try again or \"q\" to quit");
			    inp = TextIO.getlnChar();}
			    else {
			    	TextIO.putln("wrong :(/>/>/>/>/>. If you want to try agin type \"a\", otherwise type \"q\" to quit");
			    	inp = TextIO.getlnChar();}
			
			
			    
		}
		
		TextIO.putln();
		TextIO.putln("Thanks for playing, have a nice day! :)/>/>/>/>/>");
	}
}



I was ridicilously proud of it :-D. So I thought, id expand it, make it better and what not. Maybe I could give it as a present to the daughter of a friend of mine, bacause. What is better then someone actually using what you made?

So problem is. I wanted to have some variation and exchange the "CORRECT...blabla" output after a correct answer with other answers. So, I wrote some extra stuff to paste into the program, trying to exchange that output with others using if statement and counters.

I tryed putting those "if" statements within the original "if" statement. As you can see here:


public class Multip {

	public static void main(String[]args) {
		
		int numb1;
		int numb2;
		int answ;
		int result;
		char com;
		char inp;
		int count;
		com ='a';
		
		TextIO.putln("Hello. Im a small program that will teach you multiplication." + 
                " Every time you type \"a\" you are going to get a new set of numbers to multiply." + 
                " You simply type in the result and hit enter. Go ahead and try by typing \"a\".");
		
		inp = TextIO.getlnChar();
		
		count = 0;
		
		while (inp == com) {
			numb1 = (int) (13 * Math.random());
			numb2 = (int) (13 * Math.random());
			result = numb1 * numb2;
			TextIO.put( numb1 + " * " + numb2 + " = ");
			answ = TextIO.getlnInt();
			TextIO.putln();
			if (answ == result) {
				TextIO.putln("CORRECT!! type \"a\" to try again or \"q\" to quit");
				count++;
			    inp = TextIO.getlnChar();                 //This is what I begunn with. to try to replace
			    if (count == 5){                          // with other messages every fifth correct answer.
					TextIO.putln("GREAT Job!");}  //since it didnt work I tried others I forgot
				if (count == 10){                     // witch usually deteriorated to utter chaos X)
					TextIO.putln("AMAZING! keep going :)/>/>/>/>/>");}
				if (count == 15){
					TextIO.putln("You´r AWSOME!!!. Keep at it. You have almost won!!! ");}
				if (count == 20){
					TextIO.putln("Your a Multiplication GOD!!! CONGRATULATIONS!!!");
					TextIO.putln("You have won a PRIZE!!!");
					TextIO.putln();
					TextIO.putln("Just tell your Father this code:   W3B7");}
				}
			    else {
			    	TextIO.putln("wrong :(/>/>/>/>/>. If you want to try agin type \"a\", otherwise type \"q\" to quit");
			    	inp = TextIO.getlnChar();}
			
			
			    
		}
		
		TextIO.putln();
		TextIO.putln("Thanks for playing, have a nice day! :)/>/>/>/>/>");
	}
}



But then the new output like AWSOME! etc, just get added to the "CORRECT!" output, rather then replacing it.
I tryed if/else statements as well within the original if statement. Sort of:

if (answ == result) {
if (count == 5){
TextIO.putln("AWSOME!")}
else {
TextIO.putln("CORRECT!! type....")

I even tryed some fancy if ((answ == result) && (count != 5 || 10 || 15 || 20))
TextIO.putln ("CORRECT!! type....")

to sort of stop the "CORRECT" output to show when counter is set to trigger the other outputs.
But got error message that operand || is undefined for the argument type int, int.
sometimes it said something about boolean.

Anyway. I messed it all up. at the end it wouldnt even aknowledge when you typed the right answer :-/.

Today I tryed again with no success. And spent my time finding original that worked plus this first draft of extended version.
I really feel like I hit a brick wall here.

I Hope all this makes sence. That it shows somewhat what little I know and what Im trying to do.

Hoping to learn some by your answers :)/>/>/>/>

Is This A Good Question/Topic? 0
  • +

Replies To: Replacing one output with another using counter and if

#2 Maynia  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 48
  • Joined: 17-March 09

Re: Replacing one output with another using counter and if

Posted 15 December 2013 - 06:45 PM

Quote

I even tryed some fancy if ((answ == result) && (count != 5 || 10 || 15 || 20))
TextIO.putln ("CORRECT!! type....")


What the compiler was trying to tell you about this is that Boolean OR (|| in Java) can only be applied to statements that evaluate to 'true' or 'false'.

count != 5

is such a statement - evaluating it will always result in true or false.

These on the other hand:
10 || 15 || 20

are straight integers and cannot be evaluated as Boolean values (does the number 5 represent true or false? We can't tell!). If you're going to do it this way, you need to explicitly make the comparison in every OR option.

if (thing == 1 || (thing - 2) == 5 || thing == -34)


See the difference? All parts of the compound OR statement can be individually evaluated, and thus the whole statement can.

With regard to the other obvious option, you want the if to pick up any of a number of possible options, but if it finds none of them fall back to a default. You've already used one else statement in this code, it can be applied just as easily to this problem. If you take this route, though, you need to remove the generic statement from everywhere but the else statement or it'll get output multiple times.

This post has been edited by Maynia: 15 December 2013 - 06:52 PM

Was This Post Helpful? 0
  • +
  • -

#3 JNoobie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-December 13

Re: Replacing one output with another using counter and if

Posted 15 December 2013 - 07:45 PM

Ah. Could one say then in laymansterms that && and || operators only work between parentesis. Like:

        if (x < 10) || (y ==5)

        if (x > y) && (y <= 5)



?
Was This Post Helpful? 0
  • +
  • -

#4 JNoobie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-December 13

Re: Replacing one output with another using counter and if

Posted 15 December 2013 - 08:19 PM

Yeah. apparently I didnt understand the last part.

I made the generic in an "else" statement. And (since beginner) not to make it complicated and keep the original "else" change it with an if ( answ != result) to output when wrong. Was afraid to use two "else"´s in a row heh.

but f##k. Now I dont get any feedback at all :-O :( .

was It something like that you meant? Am I close or way off?. Starting to feel retarded by now LoL.

Oops forgot the change I made. This how it looks now:

public class Multip {

	public static void main(String[]args) {
		
		int numb1;
		int numb2;
		int answ;
		int result;
		char com;
		char inp;
		int count;
		com ='a';
		
		TextIO.putln("Hello. Im a small program that will teach you multiplication." + 
                " Every time you type \"a\" you are going to get a new set of numbers to multiply." + 
                " You simply type in the result and hit enter. Go ahead and try by typing \"a\".");
		
		inp = TextIO.getlnChar();
		
		count = 0;
		
		while (inp == com) {
			numb1 = (int) (13 * Math.random());
			numb2 = (int) (13 * Math.random());
			result = numb1 * numb2;
			TextIO.put( numb1 + " * " + numb2 + " = ");
			answ = TextIO.getlnInt();
			TextIO.putln();
			if (answ == result) {
				
			    if (count == 5){
					TextIO.putln("GREAT Job!");
					inp = TextIO.getlnChar();
					count++;}
				if (count == 10){
					TextIO.putln("AMAZING! keep going :)/>/>");
					inp = TextIO.getlnChar();
					count++;}
				if (count == 15){
					TextIO.putln("You´r AWSOME!!!. Keep at it. You have almost won!!! ");
					inp = TextIO.getlnChar();
					count++;}
				if (count == 20){
					TextIO.putln("Your a Multiplication GOD!!! CONGRATULATIONS!!!");
					TextIO.putln("You have won a PRIZE!!!");
					TextIO.putln();
					TextIO.putln("Just tell your Father this code:   W3B7");
					inp = TextIO.getlnChar();
					count++;}
				}
			else {
				TextIO.putln("CORRECT!! type \"a\" to try again or \"q\" to quit");
				count++;
			    inp = TextIO.getlnChar();}
			
			if ( answ != result){
				TextIO.putln("wrong :(/>/>. If you want to try agin type \"a\", otherwise type \"q\" to quit");
		    	inp = TextIO.getlnChar();}
		
			
				
				
			}
			   

		TextIO.putln();
		TextIO.putln("Thanks for playing, have a nice day! :)/>/>");	    
			
			
			    
		}
		
		
	}



Was This Post Helpful? 0
  • +
  • -

#5 Maynia  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 48
  • Joined: 17-March 09

Re: Replacing one output with another using counter and if

Posted 16 December 2013 - 04:47 AM

Nesting ifs and indeed nesting generally can be a bit confusing at first, but if you keep doing it you'll get a feel for flow of control and how it works.

On topic, I think I might have misunderstood the problem a touch (don't post while tired! :whistling:)

What you have now does this:

IF the answer given is correct:
Check if it matches one of the predefined triggers and if so print a message about that

OTHERWISE:
Print the generic correct message
IF the answer is not correct print the failure message


We'll leave compound Boolean statements alone for the moment and try and get this logic right.

In English, what you want to do is presumably more like this:

IF the answer given is correct:
IF one of the predefined triggers matches print a specific message
OTHERWISE: Print the generic correct message


OTHERWISE: Print the failure message

In the event that the answer is correct, we want exactly one thing to happen, whatever it might be. So entirely within the first if, we can use this structure:

if (answer == correctanswer) {
  if (noOfCorrectAnswers == 2) {
    doOneThing();
  }
  else if (noOfCorrectAnswers == 4) {
    doAnotherThing();
  }
  else {
    doAGenericThing();
  }
}
else {
  doNothing();
}


The if-else if-else structure allows us to pick from a number of specific responses - first the outer if is evaluated, and if the condition matches, we evaluate the inner if conditions - only one of these conditions can trigger, and if none of them do we end up taking the generic option. If the outer if condition doesn't match, we won't even try any of the inner ifs, because we don't need to.

I'm aware this is a bit rambly, because I don't know exactly what your level is, but I hope it helps illuminate a bit more - I should really have mentioned else-ifs in the first post.

This post has been edited by Maynia: 16 December 2013 - 04:50 AM

Was This Post Helpful? 0
  • +
  • -

#6 JNoobie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-December 13

Re: Replacing one output with another using counter and if

Posted 17 December 2013 - 07:08 PM

Howdy again Maynia. First of all I want to thank you for giving some time to my problem, secondly
to appologize awell. I really though I was supposed to fix the stuff after the chapters I read.
Im the kind that wants to try alot before giving up.

Anyway. The reason Im appologizing is because just a couple of subchapters ahead, it was written moe about the very thing YOU Mentioned. The else if´s :bigsmile:/> .

Anyway. It does what I want it to do now. and this is what it looks like if your interested hehe.



public class Multip {

	public static void main(String[]args) {
		
		int numb1;
		int numb2;
		int answ;
		int result;
		char com;
		char inp;
		int count;
		com ='a';
		
                TextIO.putln();

		TextIO.putln("               (..._______-------**=**-------_______...)");
		
                TextIO.putln();
                
		TextIO.putln(" Hello. Im a small program that will teach you multiplication.");
		TextIO.putln(" Every time you type \"a\" you are going to get a new set of numbers to multiply."); 
		TextIO.putln(" You simply type in the result and hit enter. Go ahead and try by typing \"a\".");
		
		TextIO.putln();
		
		inp = TextIO.getlnChar();
		
		count = 0;
		
		while (inp == com) {
			numb1 = (int) (13 * Math.random());
			numb2 = (int) (13 * Math.random());
			result = numb1 * numb2;
			TextIO.put( numb1 + " * " + numb2 + " = ");
			answ = TextIO.getlnInt();
			TextIO.putln();
			
			if (answ != result) {
				TextIO.putln("wrong :(/>/>. If you want to try agin type \"a\", otherwise type \"q\" to quit");
		    	inp = TextIO.getlnChar();
		    	count = 0;}
			else if (answ == result && count == 20) {
				TextIO.putln("Your a Multiplication GOD!!! CONGRATULATIONS!!!");
				TextIO.putln("You have won a PRIZE!!!");
				TextIO.putln();
				TextIO.putln("Just tell your Father this code:   R2D2");
				inp = TextIO.getlnChar();
				count++;
			}
			
			else if (answ == result && count == 15){
				TextIO.putln("You´r AWSOME!!!. Keep at it. You have almost won!!! ");
				inp = TextIO.getlnChar();
				count++;
			}
			
			else if (answ == result && count == 10){
				TextIO.putln("AMAZING! keep going :)/>/>");
				inp = TextIO.getlnChar();
				count++;
			}
			
			else if (answ == result && count == 5){
				TextIO.putln("GREAT Job!");
				inp = TextIO.getlnChar();
				count++;
			}
			
			else if (answ == result){
				TextIO.putln("CORRECT!! type \"a\" to try again or \"q\" to quit");
				count++;
			    inp = TextIO.getlnChar();
			}
			
		
			
				
				
			}
			   

		TextIO.putln();
		TextIO.putln("Thanks for playing, have a nice day! :)/>/>");	    
			
			
			    
		}
		
		
	}




Man, programming is HARD. "Oh, Ill get this by the end of the week" ends up in: "after 2 weeks I know only 1/3 of it :-O" Maybe not cut out for this :-O. Not even shure if this is the right forum for a total newb like me? :unsure:/> Cuz there´s something I need clarifyed like every day lol.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1