gpt4 book ai didi

java - (作业)运行,但没有返回我需要的值

转载 作者:行者123 更新时间:2023-12-02 06:09:48 25 4
gpt4 key购买 nike

我对编程很陌生。我知道循环会让这看起来更有效率。但现在,我只需要弄清楚如何返回正确的值。我需要根据它们的值(value)找出最好的动物。应返回最高值。

  • 我已将语句中的比较代码更改为 .equals,所以现在 Surveyresponse.equals("answer") 它并没有解决我的问题。

  • 代码当前正在运行,但仅返回 dragon 作为答案

  • 我需要代码来返回具有最高值(value)的动物。
  • 任务要求狗赢得所有平局,而猫总是输掉平局。

    import java.util.Scanner;

    public class PetSurvey{
    public static void main(String[] args){
    String surveyResponse;
    String y;
    int dogScore, catScore, dragonScore, z;
    dogScore = 0;
    catScore = 0;
    dragonScore = 0;
    z = 0;
    y = "best animal";

    Scanner foo = new Scanner( System.in );
    System.out.print(" What is your favorite pet? ");
    System.out.print(" a) Dogs ");
    System.out.print(" b) Cats ");
    System.out.print(" c) Dragons ");
    System.out.print(" d) I like cats, dogs and dragons ");
    surveyResponse = foo.next();
    if (surveyResponse == "a"){
    dogScore = dogScore + 3;}
    if (surveyResponse == "b"){
    catScore = catScore + 3;}
    if (surveyResponse == "c"){
    dragonScore = dragonScore + 3;}
    if (surveyResponse == "d"){
    dogScore = dogScore + 1;
    catScore = catScore + 1;
    dragonScore = dragonScore +1;}

    System.out.print(" What is your favorite pet? ");
    System.out.print(" a) Dogs ");
    System.out.print(" b) Cats ");
    System.out.print(" c) Dragons ");
    System.out.print(" d) I like cats, dogs and dragons ");
    surveyResponse = foo.next();
    if (surveyResponse == "a"){
    dogScore = dogScore + 3;}
    if (surveyResponse == "b"){
    catScore = catScore + 3;}
    if (surveyResponse == "c"){
    dragonScore = dragonScore + 3;}
    if (surveyResponse == "d"){
    dogScore = dogScore + 1;
    catScore = catScore + 1;
    dragonScore = dragonScore +1;}

    System.out.print(" What is your favorite pet? ");
    System.out.print(" a) Dogs ");
    System.out.print(" b) Cats ");
    System.out.print(" c) Dragons ");
    System.out.print(" d) I like cats, dogs and dragons ");
    surveyResponse = foo.next();
    if (surveyResponse == "a"){
    dogScore = dogScore + 3;}
    if (surveyResponse == "b"){
    catScore = catScore + 3;}
    if (surveyResponse == "c"){
    dragonScore = dragonScore + 3;}
    if (surveyResponse == "d"){
    dogScore = dogScore + 1;
    catScore = catScore + 1;
    dragonScore = dragonScore +1;}

    if (dogScore > z){
    z = dogScore;}
    if (catScore > z){
    z = catScore;}
    if (dragonScore > z){
    z = dragonScore;}
    if (dogScore == catScore){
    z = dogScore;}
    if (catScore == dragonScore){
    z = dragonScore;}

    if (z == dogScore){
    y = "dog";}
    if (z == catScore){
    y = "cat";}
    if (z == dragonScore){
    y = "dragon";}

    System.out.print(" The most popular pet is: " + y + ".");
    }

    }

如果有任何帮助,我将不胜感激,提前致谢。

最佳答案

看来您的问题可能与您在代码末尾比较分数的方式有关。

您可以通过首先将狗的分数与其他分数进行比较(因为狗赢得所有平局),然后将龙的分数与猫的分数进行比较(因为猫失去所有平局),从而大大简化事情,如下所示:

if (dogScore >= catScore && dogScore >= dragonScore) {
y = "dog";
}
else if (dragonScore >= catScore) {
y = "dragon";
}
else {
y = "cat";
}

这样您就可以完全摆脱 z 并只专注于比较每只动物的实际分数。

我希望这会有所帮助,但如果不清楚,请告诉我。

关于java - (作业)运行,但没有返回我需要的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21985964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com