2015-03-10 4 views
-4

До сих пор я был в состоянии сделать методы и позволить пользователю немедленно выйти. То, что у меня были проблемы с это ....Как переключиться с одного метода на другой с помощью оператора switch или цикла?

  1. Имея пользователю выбрать операцию (сложение, вычитание, умножение, деление)
  2. Имея способ выбрать операцию
  3. Затем идет с утверждение getNumber и различные операции

Edit: спасибо за помощь dawner с getNumber, я должен вам Вот то, что я до сих пор

import java.util.*; 
import java.security.*; 
public class Project6 
{ 
    public static void main(String[] args) 
    { 
    boolean Go = true; 
    while(Go) 
    { 
    Scanner input = new Scanner(System.in); 
    System.out.println("Choose an operation"); 
    System.out.println("1 for Addition"); 
    System.out.println("2 for Subtraction"); 
    System.out.println("3 for Multiplication"); 
    System.out.println("4 for Division"); 
    System.out.println("5 to Quit"); 
    int response=input.nextInt(); 
    if(response == 5) 
    { 
     System.out.print("You have quit the calculator"); 
     Go = false; 
    } 
    Operation(response); 
    } 
} 
public static void Operation(int response) 
{ 
    switch(response) 
    { 
     case 1: add(); break; 
     case 2: subtract(); break; 
     case 3: multiply(); break; 
     case 4: divide(); break; 
     default: System.out.println("Invalid Response"); 
    } 
} 
public static void getNumber() 
{ 
    try 
    { 
     SecureRandom num = SecureRandom.getInstance("SHA1PRNG"); 
     int digit1; 
     digit1 = num.nextInt(); 
     int digit2; 
     digit2 = num.nextInt(); 
    } 
    catch(NoSuchAlgorithmException nsae) 
    { 
     System.out.println("Is SHA1PRNG spelled correctly?"); 
    } 
} 
public static void add(int digit1, int digit2) 
{ 
    Scanner input = new Scanner(System.in); 
    int sum; 
    sum = digit1 + digit2; 
    System.out.println("What is " + digit1 + " + " + digit2 + " ?"); 
    int ans; 
    ans = input.nextInt(); 
    if(ans == sum) 
    { 
     correctPrompt(); 
    } 
    else 
    { 
     incorrectPrompt(); 
    } 
} 
public static void subtract(int digit1, int digit2) 
{ 
    Scanner input = new Scanner(System.in); 
    if(digit2 > digit1) 
    { 
     int temp = digit1; 
     digit1 = digit2; 
     digit2 = temp; 
    } 
    int diff = digit1 - digit2; 
    System.out.println("What is " + digit1 + " - " + digit2 + " ?"); 
    int ans; 
    ans = input.nextInt(); 
    if(ans == diff) 
    { 
      correctPrompt(); 
    } 
    else 
    { 
      incorrectPrompt(); 
    } 
} 
public static void multiply(int digit1, int digit2) 
{ 
    Scanner input = new Scanner(System.in); 
    int product = digit1 * digit2; 
    System.out.println("What is " + digit1 + " * " + digit2 + " ?"); 
    int ans = input.nextInt(); 
    if(ans == product) 
    { 
     correctPrompt(); 
    } 
    else 
    { 
     incorrectPrompt(); 
    } 
} 
public static void divide(int digit1, int digit2) 
{ 
    int quotient; 
    quotient = digit1/digit2; 
    int remainder = digit1 % digit2; 
    if(digit2 > digit1) 
    { 
     int temp = digit1; 
     digit1 = digit2; 
     digit2 = temp; 
    } 
    if(remainder == 0) 
    { 
     Scanner input = new Scanner(System.in); 
     System.out.println("What is " + digit1 + "/" + digit2 + " ?"); 
     int ans = input.nextInt(); 
     System.out.println("What is the remainder?"); 
     int remainAns = input.nextInt(); 
     if(ans == quotient && remainAns == remainder) 
     { 
      correctPrompt(); 
     } 
     else 
     { 
      incorrectPrompt(); 
     } 
    } 
    else 
    { 
     Scanner input = new Scanner(System.in); 
     System.out.println("What is " + digit1 + "/" + digit2 + " ?"); 
     int ans; 
     ans = input.nextInt(); 
     if(ans == quotient) 
     { 
      correctPrompt(); 
     } 
     else 
     { 
      incorrectPrompt(); 
     } 
    } 
} 
public static void correctPrompt() 
{ 
    try 
    { 
     String response; 
     response = ""; 
     SecureRandom num = SecureRandom.getInstance("SHA1PRNG"); 
     int randResponse; 
     randResponse = num.nextInt(4); 
     switch(randResponse) 
     { 
      case 0: response = "Excellent!"; break; 
      case 1: response = "Nice Work!"; break; 
      case 2: response = "Right - you rock"; break; 
      case 3: response = "Well Done"; break; 
     } 
     System.out.println(response); 
    } 
    catch (NoSuchAlgorithmException nsae) 
    { 
     System.out.println("Is SHA1PRNG spelled correctly?"); 
    } 
} 
public static void incorrectPrompt() 
{ 
    try 
    { 
     String response= ""; 
     SecureRandom num = SecureRandom.getInstance("SHA1PRNG"); 
     int randResponse; 
     randResponse = num.nextInt(4); 
     switch(randResponse) 
     { 
      case 0: response = "No, please try again"; break; 
      case 1: response = "Oopsie! You are SO close!"; break; 
      case 2: response = "Don't give up - you can do this"; break; 
      case 3: response = "Promime no fumigare - Close, but no cigar"; break; 
     } 
     System.out.println(response); 
    } 
    catch (NoSuchAlgorithmException nsae) 
    { 
     System.out.println("Is SHA1PRNG spelled correctly?"); 
    } 
} 
} 
+0

Содержит ли этот код? Мне это не похоже. –

+0

уточните, что вы хотите видеть на выходе – Swathi

+0

, пожалуйста, сделайте домашнее задание самостоятельно. – iced

ответ

0

Самая большая проблема, которую я вижу, заключается в том, что ваш метод Operation() вызывает функции add(), subtract(), multiply() и divide() без параметров, но вы определили эти методы, чтобы принимать по 2 цифры каждый. Я предполагаю, что вы хотите иметь getNumber() для работы здесь, но getNumber в настоящее время просто определяет 2 цифры, а затем ничего не делает. Вот один из способов, по крайней мере, собрать его:

public static void Operation(int response) 
{ 
    int digit1 = getNumber(); 
    int digit2 = getNumber(); 
    switch(response) 
    { 
     case 1: add(digit1, digit2); break; 
     case 2: subtract(digit1,digit2); break; 
     case 3: multiply(digit1,digit2); break; 
     case 4: divide(digit1,digit2); break; 
     default: System.out.println("Invalid Response"); 
    } 
} 
public static int getNumber() 
{ 
    try 
    { 
     SecureRandom num = SecureRandom.getInstance("SHA1PRNG"); 
     return num.nextInt(); 
    } 
    catch(NoSuchAlgorithmException nsae) 
    { 
     System.out.println("Is SHA1PRNG spelled correctly?"); 
     return 0; 
    } 
} 
Смежные вопросы