2017-06-18 77 views
1

我對Java很陌生,只是隨處可用基礎知識。我做了某種計算器。如何重新啓動一個java程序

這裏(有可能更容易計算器,只需用什麼來給我的腦海裏工作)是我的代碼:

import java.util.Scanner; 

public class App { 
    public static void main(String[] args) { 

     Scanner input = new Scanner(System.in); 


     double second; 

     System.out.println("Please tell me a number"); 

     double first = input.nextDouble(); 
     input.nextLine(); // Consume newline left-over 

     System.out.println("You chose the number " + first); 

     System.out 
       .println("Now please choose what you want to do with the number. (divide, add, multiply or subtract)"); 
     String text = input.nextLine(); 

     switch (text) { 
     case "divide": 
      System.out.println("What do you want to divide your number " + first + " with?"); 
      second = input.nextDouble(); 
      double resultDivide = first/second; 
      System.out.println(first + "/" + second + " = " + resultDivide); 
      break; 
     case "add": 
      System.out.println("What do you want to add to your number " + first + "?"); 
      second = input.nextDouble(); 
      double resultAdd = first + second; 
      System.out.println(first + " + " + second + " = " + resultAdd); 
      break; 
     case "multiply": 
      System.out.println("What do you want to multiply your number " + first + " with?"); 
      second = input.nextDouble(); 
      double resultMultiply = first * second; 
      System.out.println(first + " * " + second + " = " + resultMultiply); 
      break; 
     case "subtract": 
      System.out.println("What do you want to subtract your number " + first + " with?"); 
      second = input.nextDouble(); 
      double resultSubtract = first - second; 
      System.out.println(first + " - " + second + " = " + resultSubtract); 
      break; 
     default: 
      System.out.println("Sorry, I did not quite understand that. Please try again."); 

     } 

    } 

所以我的問題是:當它在開關case語句達到默認情況下,我可以做該程序從重新啓動?

在此先感謝。

+0

中斷並繼續可選地接受一個參數。 –

+2

你可以在代碼的主要部分放一個'while'或'do ... while'循環。 – Poohl

+1

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html –

回答

0

A while循環是你需要的。循環的輸入條件將是default的情況。在循環中,你必須把這段代碼你使用之前:

System.out.println("Now please choose what you want to do with the number. (divide, add, multiply or subtract)"); 
     String text = input.nextLine(); 

因此,將繼續要求輸入,直到你沒有得到一個default情況。

1

似乎所有你想要做的是循環直到用戶指定了一個有效的操作。在這種情況下,你可以使用while循環,看一個變量:

import java.util.Scanner; 

public class App { 
    public static void main(String[] args) { 

     Scanner input = new Scanner(System.in); 

     double second; 
     System.out.println("Please tell me a number"); 
     double first = input.nextDouble(); 
     input.nextLine(); // Consume newline left-over 

     System.out.println("You chose the number " + first); 

     System.out 
       .println("Now please choose what you want to do with the number. (divide, add, multiply or subtract)"); 
     String text = input.nextLine(); 

     boolean correctOperation = true; 

     do { 
      correctOperation = true; 

      switch (text) { 
       case "divide": 
        System.out.println("What do you want to divide your number " + first + " with?"); 
        second = input.nextDouble(); 
        double resultDivide = first/second; 
        System.out.println(first + "/" + second + " = " + resultDivide); 
        break; 
       case "add": 
        System.out.println("What do you want to add to your number " + first + "?"); 
        second = input.nextDouble(); 
        double resultAdd = first + second; 
        System.out.println(first + " + " + second + " = " + resultAdd); 
        break; 
       case "multiply": 
        System.out.println("What do you want to multiply your number " + first + " with?"); 
        second = input.nextDouble(); 
        double resultMultiply = first * second; 
        System.out.println(first + " * " + second + " = " + resultMultiply); 
        break; 
       case "subtract": 
        System.out.println("What do you want to subtract your number " + first + " with?"); 
        second = input.nextDouble(); 
        double resultSubtract = first - second; 
        System.out.println(first + " - " + second + " = " + resultSubtract); 
        break; 
       default: 
        System.out.println("Sorry, I did not quite understand that. Please try again."); 
        correctOperation = false; 
        text = input.nextLine(); 
      } 
     } while(!correctOperation) 
    } 
} 
0

我認爲你需要一個while循環,例如:

String text = input.readLine(); 
while(text != null) { 
    switch(...) 
} 

多了,我想給你一些建議:

  • 你應該檢查輸入的「text」字符串是否爲null,如果「text」字符串爲null,則會遇到NullPointerException。
  • 如果你會使用這麼多的case語句,你必須考慮設計patten,鏈接是Replacing if else statement with pattern
相關問題