2017-07-11 57 views
1

我試圖在Java上創建一個簡單的計算器程序,作爲學習Java的簡單的第一個項目。while循環不停地啓動if語句

問題:我運行該程序後,我做了計算後,我想讓用戶選擇是否要結束程序或做更多的計算。由於某種原因程序沒有使用我已經放入的(if)語句,它似乎跳過它並結束我的程序,而不允許用戶在最後輸入他的選擇。

如果我的問題不清楚,我找不到真正的抱歉,我無法在線找到問題的解決方案,如果我的代碼看起來很雜亂,我會道歉。

import java.util.Scanner; 

public class Calculator { 

    public static void main(String[] args) { 

     Scanner input = new Scanner(System.in); 
     double fnum, snum, answer; 
     String choice, name, in; 

     System.out.println("Hello, whats your name?"); 
     name = input.nextLine(); 
     System.out.println(""); 
     System.out.println("Oh so your name is " + name + "!"); 
     System.out.println(""); 
     System.out.println("This program is a calculator that will do simple calculation of two numbers"); 
     System.out.println("There are four different options to choose from:"); 

     boolean running = true; 

     CAL: 
      while (running) { 
       System.out.println("type a for Addition, b for subtraction, c for multiplication and d for division"); 
       System.out.println("Then press enter!"); 

       choice = input.nextLine(); 

       while (!choice.equals("a") && 
         !choice.equals("A") && 
         !choice.equals("b") && 
         !choice.equals("B") && 
         !choice.equals("c") && 
         !choice.equals("C") && 
         !choice.equals("d") && 
         !choice.equals("D")) { 
        System.out.println("Wrong choice, Please try again"); 
        System.out.println("type a for Addition, b for subtraction, c for multiplication and d for division"); 

        choice = input.nextLine(); 
       } 

       if (choice.equals("a") || choice.equals("A")) { 

        System.out.println(name +" You have chosen Addition"); 
        System.out.println("Type the first number:"); 
        fnum = input.nextDouble(); 
        System.out.println("Type the second number:"); 
        snum = input.nextDouble(); 

        System.out.println("your answer is:"); 
        Addition addy = new Addition(fnum,snum); 
        System.out.println(addy.getans()); 

       } 

       if (choice.equals("b") || choice.equals("B")) { 

        System.out.println(name +" You have chosen Subtraction"); 
        System.out.println("Type the first number:"); 
        fnum = input.nextDouble(); 
        System.out.println("Type the second number:"); 
        snum = input.nextDouble(); 
        answer = fnum - snum; 
        System.out.println("your answer is:" 
          + answer); 

       } 

       if (choice.equals("c") || choice.equals("C")) { 

        System.out.println(name +" You have chosen Multiplication"); 
        System.out.println("Type the first number:"); 
        fnum = input.nextDouble(); 
        System.out.println("Type the second number:"); 
        snum = input.nextDouble(); 
        answer = fnum * snum; 
        System.out.println("your answer is:" 
          + answer); 

       } 

       if (choice.equals("d") || choice.equals("D")) { 

        System.out.println(name +" You have chosen Addition"); 
        System.out.println("Type the first number:"); 
        fnum = input.nextDouble(); 

        while (fnum == 0) { 
         System.out.println("invalid try again!"); 
         fnum = input.nextDouble(); 
        } 

        System.out.println("Type the second number:"); 
        snum = input.nextDouble(); 
        answer = fnum/snum; 

        System.out.println("your answer is:" 
          + answer);    
       } 

       System.out.println(""); 
       System.out.println("Thank you " + name + " for using this simple calculator :)"); 

       System.out.println("If you would like to try again press a the press Enter, if you wish to exit press any botton and then press enter"); 

       in = input.nextLine(); 

       if (in.equals("a")) { 
        System.out.println(""); 
        System.out.println("Thank you, please choose again"); 
        System.out.println(""); 
       } else { 
        System.out.println("Thank you and goodbye"); 
        break; 
       } 
      } 
    }  
} 

回答

0

這是一個由nextInt/Double/etc行爲引起的簡單問題。

這些方法不會讀取以下換行符,所以nextLine將始終返回一個空字符串(當前行的其餘部分)。

嘗試改變那些(例如加時)與Double.parseDouble(input.nextLine());

if (choice.equals("a") || choice.equals("A")) { 
     System.out.println(name + " You have chosen Addition"); 
     System.out.println("Type the first number:"); 
     fnum = Double.parseDouble(input.nextLine()); 
     System.out.println("Type the second number:"); 
     snum = Double.parseDouble(input.nextLine()); 
     System.out.println("your answer is:"); 
     Addition addy = new Addition(fnum, snum); 
     System.out.println(addy.getans()); 
    } 

你可以看到調試,最後in = input.nextLine();打電話詢問用戶輸入將永遠是空字符串。

+0

謝謝,這解決了我的問題。 –