Java跳过while语句,直接到最后

标签 java

这是我做计算机编程的第一个学期,我们的教授在中类时完全放弃了我们。但我几乎完成了类作业,但由于某种原因,我的 while 语句被跳过了。

import java.util.Scanner;
import java.text.DecimalFormat;

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

            DecimalFormat f = new DecimalFormat("##.00");

            float votesForPolly; 
            float votesForErnest; 
            float totalPolly = 0; 
            float totalErnest = 0; 
            String response; 
            int precinctsforpolly = 0; 
            int precinctsforernest = 0;
            int precinctsties = 0;

            Scanner scan = new Scanner(System.in);
            System.out.println ();      
            System.out.println ("Election Day Vote Counting Program");
            System.out.println ();


            do
            {   
                System.out.println("Do you wish to enter more votes? Enter y:n");
                response = scan.next();
                //this is where it skips from here*********
                while (response == "y")
                {
                    System.out.println("Enter votes for Polly:");
                    votesForPolly = scan.nextInt();
                    System.out.println("Enter votes for Ernest:");
                    votesForErnest = scan.nextInt();

                    totalPolly = totalPolly + votesForPolly;
                    totalErnest = totalErnest +  votesForErnest;

                    System.out.println("Do you wish to add precincts? Enter y:n");
                    response = scan.next();
                    if (response =="y")
                    {   
                        System.out.println("How many precincts voted for Polly: ");
                        precinctsforpolly = scan.nextInt();
                        System.out.println("How many precincts votes for Ernest: ");
                        precinctsforernest = scan.nextInt();
                        System.out.println("How many were ties: ");
                        precinctsties = scan.nextInt(); 
                    }
                }
            }
            while (response == "n");
            //to here*********************************************
            System.out.println("Final Tally");
            System.out.println("Polly received:\t " + totalPolly + " votes\t" + f.format((totalPolly/(totalPolly + totalErnest))*100) + "%\t" + precinctsforpolly + " precincts");
            System.out.println("Ernest received: " + totalErnest + " votes\t" + f.format((totalErnest/(totalPolly + totalErnest))*100) + "%\t" + precinctsforernest + " precincts");
            System.out.println("\t\t\t\t\t" + precinctsties + " precincts tied");

        }
}

最佳答案

字符串实际上指向 Java 中的值,但它们本身并不是值。尝试使用

while (response.equals("y"))

而不是

while (response == "y")

在前一种情况下,您告诉运行时实际比较这些值。后一种情况是告诉运行时比较指针,但实际上可能不匹配。

关于Java跳过while语句,直接到最后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43550993/

相关文章:

java - 在 selenium webdriver 中导航回时,Firefox 浏览器无法恢复页面

java - 为什么我的 Java 扫描仪不接受输入?

Java 8 : replace anonymous class with lambda

java - while(true) 和集合

java - 如何使用try block 中声明的变量?

java - 使用数组将罗马数字转换为阿拉伯格式时遇到问题

java - Spring Boot中使用YAML配置初始化Bean

java - java中基于xml的自动类模型创建

java - 延迟影响其余指令

Java 正则表达式匹配 <li> 前面没有 <br/>