![]() |
|
My second Java program - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Java, JVM, & JRE (https://sinister.li/Forum-Java-JVM-JRE) +--- Thread: My second Java program (/Thread-My-second-Java-program) Pages:
1
2
|
My second Java program - RaccoonCity_mybb_import13707 - 11-16-2013 Hey guys, as some of you guys know, I've started programming. Well, I'm REALLY proud of this program. I've been coding on my way to school and everything. This is my calculator, I've made this completely on my own! Code: import java.util.Scanner;
public class Application {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Addition = add");
System.out.println("Subtraction = sub");
System.out.println("Multiplication = multi");
System.out.println(" ");
System.out.println("Choose any of the following numbers above.");
String value = scanner.nextLine();
int c;
if ("add".equals(value)) {
System.out.println("Enter any number you want to add.");
int value1 = scanner.nextInt();
System.out.println("Enter a second number you want to add.");
int value12 = scanner.nextInt();
c = value1+value12;
System.out.println("" + value1 + " + " + value12 + " = " +c);
return;
}
else{
}
if ("sub".equals(value)) {
System.out.println("Enter a number you want to subtract.");
int value2 = scanner.nextInt();
System.out.println("Enter a second number you want to subtract.");
int value12 = scanner.nextInt();
c = value2-value12;
System.out.println("" +value2 +" - " +value12 +" = " +c);
return;
}
if ("multi".equals(value)) {
System.out.println("Enter a number you want to multiplicate.");
int value3 = scanner.nextInt();
System.out.println("Enter a second number you want to multiplicate.");
int value12 = scanner.nextInt();
c = value3*value12;
System.out.println("" +value3 +" * " +value12 +" = " +c);
return;
}
else{
System.out.println("You have to choose a number!");
}
}
}Thanks. RE: My second Java program - Slarek - 11-16-2013 You could put this in a loop so when user's input is wrong, it wouldn't close the program but showed the menu again. RE: My second Java program - blackeagle - 11-16-2013 good job my friend but this isn't very challenging but since ur new to programming ii guess it will be a good start !!as @Slarek told you make a for loop for the user choice (when he choose add or anything else the user should choose an integer but what if he just wrote d or f or any word instead of a number ? the program will close !! if you use a for loop every time the user choose something different then a number he will get a msg for exemple pnly numberrs are allowed choose a number !! this will repeat until the user choose a number then it will continue ) hope this helped !! RE: My second Java program - Slarek - 11-16-2013 (11-16-2013, 03:32 PM)blackeagle Wrote: good job my friend but this isn't very challenging While loop would be more handy IMO RE: My second Java program - RaccoonCity_mybb_import13707 - 11-16-2013 (11-16-2013, 02:47 PM)Slarek Wrote: You could put this in a loop so when user's input is wrong, it wouldn't close the Yeah, I'll try to do this with a for loop, thanks! RE: My second Java program - Slarek - 11-16-2013 (11-16-2013, 03:44 PM)Zedex Wrote:(11-16-2013, 02:47 PM)Slarek Wrote: You could put this in a loop so when user's input is wrong, it wouldn't close the Why would you put it in a for loop? Think about it, while is easier since you don't know many times input is wrong. RE: My second Java program - RaccoonCity_mybb_import13707 - 11-16-2013 (11-16-2013, 03:48 PM)Slarek Wrote:(11-16-2013, 03:44 PM)Zedex Wrote:(11-16-2013, 02:47 PM)Slarek Wrote: You could put this in a loop so when user's input is wrong, it wouldn't close the Actually, I can try to do it in a while loop, not sure how to use it in this context though, can you give me some good example on how to use it? RE: My second Java program - Solixious - 11-16-2013 Actually since this is a menu driven program, I would suggest to use a do-while loop instead of for loop or while loop. The former is an exit control loop which would execute atleast once while the latter is an entry control loop which may or may not execute in the first instance depending on the condition. Of course this can be done using any loop, but I would suggest do-while. ![]() Code: do {
//your stuffs in here
//ask the user to choose if s/he wants to continue
}
while(condition depending on user's choice);Edit : added the code structure of loop and a slight change in original post RE: My second Java program - RaccoonCity_mybb_import13707 - 11-16-2013 (11-16-2013, 03:53 PM)The Arcanist Wrote: Actually since this is a menu driven program, I would suggest to use a do-while loop instead of for loop or while loop. The former is an exit control loop which would execute atleast once while the latter is an exit control loop which may or may not execute in the first instance depending on the condition. Of course this can be done using any loop, but I would suggest do-while. Aha, I've never heard of a "do-while" loop. I'll Google this now ![]() Thanks. RE: My second Java program - Slarek - 11-16-2013 @Zedex This is just and example: Code: while(true)
{
1.Do you want to continue?
2.Do you want to quit?
If(answer == 1)
{
your program here
}
else if(answer == 2)
{
return false // or break
}
}Edit: Indeed a do-while would be handy |