How To Turn If Statement Into A Case / Switch Statement
Jun 19, 2014
So from what iv learnt in Java and programming in general is that using a case statement is far more efficient that using multiple IF statements. I have an multiple IF statements contained within a method of my program, and would like to instead use a case statement.
I am stuck with two switch statements that I feel I am close to achieving. My first switch statement is to generate random mathematical operators for the math questions in the game (generated randomly) My second switch statement is to determines which action the system will take upon the correct or incorrect answer. For the first switch statement I have a variable called num. Num is a random integer. I have an operator mapped to each outcome of num. In my second switch statement I am trying to use the variable answer as a variable in the first case but do not know how.
package pkgnew; import java.util.Scanner; import java.util.Random; public class New { public static void main(String args[]) { //Declare and construct variables
So I want to make a simple Java that ask the user to pick a powers and it has two options.If the user picks magic then execute the first if statement then ask the user again which type of magic the user wants.I can't make it work it keeps printing the else statement. Why is that?
import java.util.Scanner; public class Variable { static Scanner zcan = new Scanner(System.in); public static void main(String[] args)
this is what code i have so far i want to try and get the computer to output a String type result rather than a number and i wonder if there is a way to output the score after the user is done with the game. I couldn't really see if there was a way to incorporate a switch statement.
import java.util.Scanner; import java.util.Random; public class RPSLS { public static void main(String[] args) {
I am trying to add a while loop into my switch statement. If you run the program, it will ask to enter the class grade (9,10,11, or 12). If you insert 5, it will say to try again. But, if you enter a wrong number twice, it will continue on to the next part of the program, which is asking how many students are in the class. Therefore, I believe a while loop would work, but it is not working at all. The program still runs, just doesn't fix the error. The program is below:
import java.util.Scanner; public class stephProject { public static void main(String[] args) { //call method welcomeMessage(); //method 1 of 3
Write an application that reads the quantity for each product until user has completed their order. Your program should use switch, if, for, while and do while statements to read, calculate and display the total retail value of all pro ducts sold for each user transaction. The user should be able to start a new transaction after the first transaction is completed. The user can make no more than 3 total orders
public class Merchandise { private int bootsQTY; private int wranglersQTY; private int hatsQTY; private int chapsQTY;
[Code] ....
When I run my program all of my quantities and the total are 0, I have been stuck for a while trying to figure out how to get values assigned to them...
cannot break from while loop. Whenever I am trying to exit from startCustomerManagement-> backEnd() -> mainScreen()..It gets stuck between mainScreen and backEnd screen. However I can exit from backEnd()->startCustomerManagement() screen
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package javaapplication19; import java.io.BufferedReader;
What I want to happen is to add 2+ contacts by entering "a" from the menu. Then I want to enter "c" and have it display those contacts. But it doesn't seem to remember that I changed the arrayList masterList.
Java Code:
public class ContactListApp2 { public static void main(String[] args) { boolean quit = false; while (quit == false) { System.out.println("Main Menu:");
I'm trying to create a switch statement inside of a counter controlled (while) loop that asks for an input of...
"How many characters would you like to convert?"
Then you type in a number > 0, and then it should convert letters into the "1337" equivalent.
This is an example on what it should do.
How many characters would you like to convert? 5 Enter character #1 to convert: ! !- Enter character #2 to convert: $ $- Enter character #3 to convert: # #- Enter character #4 to convert: * *- Enter character #5 to convert: , , -
Whenever i try to run the program, i only get the- How many characters would you like to convert- i input 5 but then nothing else prints..
My codes..
System.out.print ( "How many characters would you like to convert?: " );
int convertCounter = 1; char ch; ch = input.next().charAt(0); while (convertCounter > 0) switch ( ch ){
I want to use a switch or if/else statement to filter out the data I don't need for the app. This is by no means anything more than a draft because java is easier to work with than android.
I am trying to write out a program that takes numerical input from the user and converts it to a date using the English month name. I am experimenting with the method of a "switch" statement without using the "break" clause. However, I seem to be missing something, as Eclipse is telling me I have a syntax error with my block. My curly braces seem properly placed. Also, I made sure to follow guidelines to make my code fit on the screen and remain easy to read.
import acm.program.*; public class MethodsThatReturnNonNumericValues extends ConsoleProgram { public void run() { int month=readInt("Enter month number"); int day=readInt("Enter day"); int year=readInt("Enter year");
// GradeBook class uses switch statement to count A, B, C, D and F grades.
import java.util.Scanner; public class GradeBook { private String courseName; private int total; private int gradeCounter; private int aCount;
[Code] .....
Then eclipse told me that:
"Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at chapter5_control_statements_II.GradeBook.inputGrades(GradeBook.java:52) at chapter5_control_statements_II.GradeBookTest.main(GradeBookTest.java:11)"
//Use a Switch statement to determine outcomes for letter grades switch (Letter) { case 'A': outFile.println(" Good Work! You have met the pre-req for CIS 220."); break; case 'B': outFile.println("
[Code] ....
is not printing the information on the next line in my output file.
We're told to make a cash register program for 5 products using Java. It loops until "-1" is entered on the "Continue?" input dialog box.There should be a:
1. Main Menu - where the user will pick what to buy (one product at a time) like the sample code below and prompt the user if he will buy another product or not. If yes, the program will go back to the main menu to buy another product and if not, continue to the program. And it looks something like this: LEvFOwQ.png
2. Receipt - at the end of every transaction.
3. Daily Sales Report where it shows all receipts then grand total like:
Transaction 1
-subtotal
Transaction 2
-subtotal
and so on then,
GRAND TOTAL
4. Inventory Report where it shows the Items Available before the Selling, Items Sold, and Items Available after Selling in table format.
Below is the code for a simple cash register for only one product that I made but I need to upgrade it for it to work for 5 products. That code will serve as the basis for this program.make the switch or if-else statement for the main menu first and if I got it right I try doing the rest.
int userQty1 = Integer.parseInt(JOptionPane.showInputDialog( "Product ID Product Name Product Quantity Product Price" +prodID1 + " " +prodName1 + " " + prodQty1 + " " +prodPrice1 " " + and so on until prod5));
Alright so I wrote a switch statement that decides what range to print based on the letter grade input.
import java.util.Scanner; public class SwitchPractice { public static void main(String [] args) { Scanner scan = new Scanner(System.in);
[code]...
It works fine, but once it the user enters a letter grade and then displays the value, it does not prompt the user for another letter grade so that it can perform the output again. Also if I wanted to display an error message to the user if they enter an invalid letter grade how would I do that. I tried using a while loop and if statement in the switch statement but that didn't work.
why this switch code does not work for the 'Q'. Everything works fine for the other cases, i.e. 1,2,3, and 'q', but when I type in 'Q', I get the default case. Please note all the values have been defined in an earlier part of the code but for simplicity's sake I have removed them below
do { switch (choix) { //demande le nombre de patons a achete case '1':
I am making a programme for a calculator to - Divide, multiply, add and subtract. I have the code written but when I am running it and trying to add it was for some subtracting ....
import javax.swing.JOptionPane; public class calculator3 { public static void main(String[] args){ String number1,number2,operatorstring; double result=0,a,b;
int grade = 68; switch (grade) { case 100: System. out.println( "You got an A. Great job!" ); break; case 80: System. out.println( "You got a B. Good work!"); break;
If I have an integer variable like int a=9 then in the switch case If i write
switch(a) { case 4+a: System.out.println("hii"); }
Then why is this statement a compile-time error that variables cannot be used inside a case statement why does the compiler not subtitutes the values in place of the variables.
So basically what problem it creates for which the language developers did not include it as a proper syntax,is there any reason behimd this because of jump table?
I'm trying to run an if statement with an input condition and here is my code.
import java.util.Scanner; public class App10A { public static void main(String[] args) { Scanner fun = new Scanner(System.in); System.out.println("What's your name?"); String name = fun.nextLine();
[code]....
When the computer asked me "What's your name?", I typed in Victor, then the result turned out to be "Oops, try again!".I typed in Victor (without parenthesis) and "Victor" and both gave me the negative results.What exactly should I put in in order to get "This is the result I wanted!" ? how should I modify my code in order to get "This is the result I wanted!" when I put in Victor (without changing the equal sign)?