9 Replies - 385 Views - Last Post: 10 September 2012 - 09:06 AM Rate Topic: -----

#1 Thrillofit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-September 12

New in Java. Need help about my script.

Posted 10 September 2012 - 06:53 AM

import java.util.Scanner;


public class Ahmed {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int counter = 0;
		while(counter < 10){
			counter++;
		// TODO Auto-generated method stub
		System.out.println("Vilken metod vill du använda? Skriv in ett tal mellan 1-4.");
		System.out.println("1. Addition");
		System.out.println("2. Subtraktion");
		System.out.println("3. Multiplikation");
		System.out.println("4. Division");
		
		
		

		Scanner i = new Scanner(System.in);
		int number = i.nextInt();
		
		if(number!=0-4);{
			System.out.println("You have no meadow number between 1-4. Try again by pressing any number!");
			
		Scanner e = new Scanner(System.in);
		int number1 = i.nextInt();
		
		System.out.println("Which method would you use? Enter a number between 1-4.");
		System.out.println("1. Addition");
		System.out.println("2. Subtraction");
		System.out.println("3. Multiplication");
		System.out.println("4. Division");
		
		System.out.println("Enter the first number");
		double a = i.nextDouble();
		System.out.println("Enter the second number");
		double b = i.nextDouble();

		if(number==1){
		System.out.println(a+B)/>;
		}
		else if(number==2){
		System.out.println(a-B)/>;
		}
		else if(number==3){
		System.out.println(a*B)/>;
		}
		else if(number==4){
		System.out.println(a/B)/>;
		}
		}
		}
	}
}




Hey all! This is my first time here in this fourm and was so amazing thing in here. I have just started to use Java script and wanted to try to make a meny about math because i like alot math. Anyways i cant just get it right. My problem is how to make this better. I want to add a script there you can choose from 1-4 options what metod and if you write like 5 6 or something alse (not 1-4) it should come a text Try to press right number or something like that. And also if i make take 2 numbers like 3*3 = 9 and after the answear it should come like "Do you want to try again something alse, press X" or something like that but i dont know how to do it and would be very greatful if someone could help me:)

Is This A Good Question/Topic? 0
  • +

Replies To: New in Java. Need help about my script.

#2 Thrillofit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-September 12

Re: New in Java. Need help about my script.

Posted 10 September 2012 - 06:59 AM

import java.util.Scanner;


public class Ahmed {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int counter = 0;
		while(counter < 10){
			counter++;
		// TODO Auto-generated method stub
		System.out.println("Which method would you use? Enter a number between 1-4.");
		System.out.println("1. Addition");
		System.out.println("2. Subtraction");
		System.out.println("3. Multiplication");
		System.out.println("4. Division");
		
		
		

		Scanner i = new Scanner(System.in);
		int number = i.nextInt();
		
		if(number!=0-4);{
			System.out.println("You have no meadow number between 1-4. Try again by pressing any number!");
			
		Scanner e = new Scanner(System.in);
		int number1 = i.nextInt();
		
		System.out.println("Which method would you use? Enter a number between 1-4.");
		System.out.println("1. Addition");
		System.out.println("2. Subtraction");
		System.out.println("3. Multiplication");
		System.out.println("4. Division");
		
		System.out.println("Enter the first number");
		double a = i.nextDouble();
		System.out.println("Enter the second number");
		double b = i.nextDouble();

		if(number==1){
		System.out.println(a+B)/>;
		}
		else if(number==2){
		System.out.println(a-B)/>;
		}
		else if(number==3){
		System.out.println(a*B)/>;
		}
		else if(number==4){
		System.out.println(a/B)/>;
		}
		}
		}
	}
}



I made some mistake at the first post and i dont really know how to edit so i type here instead
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7575
  • View blog
  • Posts: 12,725
  • Joined: 19-March 11

Re: New in Java. Need help about my script.

Posted 10 September 2012 - 07:03 AM

Quote

And also if i make take 2 numbers like 3*3 = 9 and after the answear it should come like "Do you want to try again something alse, press X" or something like that but i dont know how to do it


Use a while loop for this. Something like


boolean gå_igen = true;  
while (gå_igen)
{  

  //  your calculator code goes in here

  // now ask the user if they want to go around again

  System.out.println ("Vill du beräkna ett annat nummer?");

  String answer = i.nextLine();
  // if they say no, we want to stop
  if answer.equals("nej")
  {
    gå_igen = false;
  }
}

Was This Post Helpful? 0
  • +
  • -

#4 Thrillofit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-September 12

Re: New in Java. Need help about my script.

Posted 10 September 2012 - 07:14 AM

View Postjon.kiparsky, on 10 September 2012 - 07:03 AM, said:

Quote

And also if i make take 2 numbers like 3*3 = 9 and after the answear it should come like "Do you want to try again something alse, press X" or something like that but i dont know how to do it


Use a while loop for this. Something like


boolean gå_igen = true;  
while (gå_igen)
{  

  //  your calculator code goes in here

  // now ask the user if they want to go around again

  System.out.println ("Vill du beräkna ett annat nummer?");

  String answer = i.nextLine();
  // if they say no, we want to stop
  if answer.equals("nej")
  {
    gå_igen = false;
  }
}

Getting error at "if answer.equals("nej")

i did like this now

import java.util.Scanner;


public class tja {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		boolean gå_igen = true;  
		while (gå_igen)
		{  

			System.out.println("Which method would you use? Enter a number between 1-4.");
			System.out.println("1. Addition");
			System.out.println("2. Subtraction");
			System.out.println("3. Multiplication");
			System.out.println("4. Division");

		  

		  System.out.println ("Vill du beräkna ett annat nummer?");
		  Scanner i = new Scanner(System.in);
			int number = i.nextInt();
		  
		String answer = i.nextLine();
		 
		if answer.equals("nej");

		  {
		    gå_igen = false;
		  }
		}

	}

}


Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7575
  • View blog
  • Posts: 12,725
  • Joined: 19-March 11

Re: New in Java. Need help about my script.

Posted 10 September 2012 - 07:42 AM

... and what happened?


(I can tell what happened from reading the code, but you have to get in the habit of asking questions and explaining what it is you need help with)
Was This Post Helpful? 0
  • +
  • -

#6 Thrillofit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-September 12

Re: New in Java. Need help about my script.

Posted 10 September 2012 - 07:57 AM

View Postjon.kiparsky, on 10 September 2012 - 07:42 AM, said:

... and what happened?


(I can tell what happened from reading the code, but you have to get in the habit of asking questions and explaining what it is you need help with)


Hello! It came a red string down the word If.
I want to make this script work by looping and if u write something alse than a number between 1-4 it should come a text saying "Please write a number between 1-4" or something like that:)

I dont know if you understood my script so i will tell it again:)
This is my first script and i want to make a meny there u can choose Addition, Subraction, Miltiplication and division. Each subjecy have each number and if you are wrtting something alse it should come a text saying try a number 1-4 again.
Now if i take number 1 which is addtion it should then come a text to pick 2 differend number and we take 1+1 and then it should automatic tell you the answer and right after it. It should come a new text saying "Want to try another method" or something like it. And by saying Yes it should take you back to the meny and if you pick no then the program will exit;)
Sorry for my bad english.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7575
  • View blog
  • Posts: 12,725
  • Joined: 19-March 11

Re: New in Java. Need help about my script.

Posted 10 September 2012 - 08:07 AM

View PostThrillofit, on 10 September 2012 - 09:57 AM, said:

View Postjon.kiparsky, on 10 September 2012 - 07:42 AM, said:

... and what happened?


(I can tell what happened from reading the code, but you have to get in the habit of asking questions and explaining what it is you need help with)


Hello! It came a red string down the word If.



Oh, that's my bad, sorry.
Should have been:
 if (answer.equals("nej"))


(I've been working in python lately, and it's hard to switch back sometimes!)


While I'm at it, this will cause a problem:
          Scanner i = new Scanner(System.in);  // don't need to make a new Scanner
            int number = i.nextInt();   //delete this line
            String answer = i.nextLine();





Quote

Sorry for my bad english.


Your English is a lot better than my Swedish!

This post has been edited by jon.kiparsky: 10 September 2012 - 08:10 AM

Was This Post Helpful? 0
  • +
  • -

#8 Thrillofit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-September 12

Re: New in Java. Need help about my script.

Posted 10 September 2012 - 08:23 AM

View Postjon.kiparsky, on 10 September 2012 - 08:07 AM, said:

View PostThrillofit, on 10 September 2012 - 09:57 AM, said:

View Postjon.kiparsky, on 10 September 2012 - 07:42 AM, said:

... and what happened?


(I can tell what happened from reading the code, but you have to get in the habit of asking questions and explaining what it is you need help with)


Hello! It came a red string down the word If.



Oh, that's my bad, sorry.
Should have been:
 if (answer.equals("nej"))


(I've been working in python lately, and it's hard to switch back sometimes!)


While I'm at it, this will cause a problem:
          Scanner i = new Scanner(System.in);  // don't need to make a new Scanner
            int number = i.nextInt();   //delete this line
            String answer = i.nextLine();





Quote

Sorry for my bad english.


Your English is a lot better than my Swedish!



OH thats why i was like "when did i write gå" ? hehe
Anyways i just cant make it work as it want.
import java.util.Scanner;


public class tja {

	private static String answer;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		boolean gå_igen = true;  
		while (gå_igen)
		{  

			System.out.println("Which method would you use? Enter a number between 1-4.");
			System.out.println("1. Addition");
			System.out.println("2. Subtraction");
			System.out.println("3. Multiplication");
			System.out.println("4. Division");
			
			Scanner i = new Scanner(System.in);
			int number = i.nextInt();
			
			System.out.println("Enter the first number");
			double a = i.nextDouble();
			System.out.println("Enter the second number");
			double b = i.nextDouble();

			if(number==1){
			System.out.println(a+B)/>;
			}
			else if(number==2){
			System.out.println(a-B)/>;
			}
			else if(number==3){
			System.out.println(a*B)/>;
			}
			else if(number==4){
			System.out.println(a/B)/>;

			
		  System.out.println ("Vill du beräkna ett annat nummer?");
		  String answer = i.nextLine();
		if (answer.equals("nej"))
			

		  {
		    gå_igen = false;
		  }
		}

	}

}
}



I cant do this anymore. would appreciate if you could send me a code like this and make it correct and then i can see what i did wrong:)
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7575
  • View blog
  • Posts: 12,725
  • Joined: 19-March 11

Re: New in Java. Need help about my script.

Posted 10 September 2012 - 08:48 AM

Quote

would appreciate if you could send me a code like this and make it correct and then i can see what i did wrong:)


Won't do that, but if you let me know what your latest problem is, I'll try to point you to an answer.
Was This Post Helpful? 0
  • +
  • -

#10 Thrillofit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-September 12

Re: New in Java. Need help about my script.

Posted 10 September 2012 - 09:06 AM

View Postjon.kiparsky, on 10 September 2012 - 08:48 AM, said:

Quote

would appreciate if you could send me a code like this and make it correct and then i can see what i did wrong:)


Won't do that, but if you let me know what your latest problem is, I'll try to point you to an answer.


Now when im taking a number over 4. it is just saying pick a number and the second and just come back after to the meny.
And when im taking addition and making it. it works i got a answer but it doesnt ask me if i want to use it again just automatic it comes the meny.

I want that over the number 4 it should come a text saying pick a number again between 1-4.

I also want if i take addition or whatever else it should come the answer and then ask if i want to do this again or back to meny. And then i have to pick Yes or no:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1