簡體   English   中英

我遇到了java if if語句嵌套在while循環中的問題

[英]I am facing problems with java if statement nested in while loop

我是初學者。

我正在嘗試使用java while循環。 我知道java while循環和if語句,但從未嵌套過它們。 我將首先發布腳本,然后告訴你們這個問題。

這是腳本:

import java.util.Scanner;
class whileloop{
    public static void main(String args[]){
        Scanner scan = new Scanner(System.in);
    System.out.println("This is a basic calculator.");
    System.out.println("To exit, input exit when asked to input the method.");
    System.out.println("----------------------------------------------");
    while (true){
        System.out.println("to add, input '+', to subtract, input '-', to divide, input '/', to multiply, input '*' and to exit, input 'exit.'");
        System.out.println("----------------------------------------------------");
        System.out.print("Please enter a method here:");
        String method = scan.nextLine();
        if (method == "exit"){
            System.out.println("You chose to exit.");
            break;
        }
        else if (method == "+"){
            System.out.println("You chose to add.");
            System.out.print("Please enter first number here:");
            double fnum = scan.nextInt();
            System.out.print("Please enter second number here:");
            double snum = scan.nextInt();
            double ans = fnum + snum;
            System.out.print("The answer is");
            System.out.println(ans);
        }
    }
}
}

首先,我不知道這種類型的嵌套我現在只是在試驗,所以也許這就是為什么我的方法對你們來說似乎有點不尋常。

現在,問題是每當我執行這段代碼時,無限循環工作完美但它總是跳過if語句,無論我輸入什么並開始再次循環指令。

請幫我解決一下這個。

采用

method.equalsIgnoreCase("exit")method == "exit"

equalsIgnoreCase比較內容,但==比較引用,在你的情況下,引用與你使用scan.nextLine(); 這會創建一個新的String對象。

比較字符串時,需要使用.equals()方法,因此,在您的情況下,您需要將method == "exit"更改為method.equals("exit") 這同樣適用於其他if語句。

您可以查看 Oracle教程,以獲取有關字符串比較的更多信息。

如果使用==來比較字符串,這將比較對象的引用,在這種情況下,它將始終返回false。 如果您實際上沒有看到這種情況,那么您沒有其他地方而只有其他地方,並且它會繼續進行下一次迭代。

將其更改為.equals()方法以比較對象值而不是引用。

邊注

請注意參考比較,因為您可能已經看到了使用引用相等性的奇怪示例,這可能會引發您(這對我來說肯定是第一次,如下面的示例所示)。

String a = "Hello";
String b = "Hello";

如果你在這種情況下比較a == b它實際上會返回true,這是因為Java緩存了字符串,因此在這種情況下它們具有相同的引用。 當你看到這樣的例子時要小心,因為它們是例外。

使用method.equals("exit")進行字符串比較。

這篇文章。

==它對象的測試引用相等。

等於(字符串)這是一種方法,String類兩個字符串的內容進行比較。

在java中, ==表示檢查reference equality ,但數字除外。 (見下面的評論)

Object obj1 = new Object();//toString: java.lang.Object@459189e1 <--|
                                                                  //|- different addresses
Object obj2 = new Object();//toString: java.lang.Object@55f33675 <--|
obj1 == obj2; //false, because they are difference objects, difference references.

obj1 = obj2;
obj1 == obj2; //true, they are now pointing to the same object

因此,您需要使用.equals來檢查字符串相等性。

這是所有初學者的問題。 我開始的時候就做過了。 這里的問題是

等於運算符==

這里不應該用來比較兩個字符串。 因為它用於檢查任何兩個對象沒有區別但是同一個實例。

Object O1 = new Object()
Object o2 = o1

這里

o1 == o2

將評估為真。 因為他們檢查參照平等。

在string的情況下,當您比較字符串的內容時,您應該始終將theme與之比較

等於

方法或

equalsIgnoreCase

如果你不為這個案子煩惱。 上帝保佑。 干杯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM