我是靠谱客的博主 忧伤萝莉,最近开发中收集的这篇文章主要介绍java while 定义变量_在while循环中声明字符串变量,而不会在整个循环中循环-Java...,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

我被困在我应该声明一个称为“ phrase”的字符串变量的部分,该变量不应一直循环播放。

让您知道我的任务是:与选项1相似,不同之处在于用户在输入第一队的结果后输入“ N”(而不是“ Q”)。然后,程序输入第二个团队名称及其结果,直到输入“

Q”。输出两个语句,例如选项1中的语句,然后输出第三条语句,该语句说明哪个团队处于第一位(基于点数)

输入样例:

2

Toronto

W

W

L

O

W

O

W

N

Montreal // how would I make this appear in the same while loop?

L

L

O

L

L

W

L

L

Q

样本输出:

Toronto has played 7 games and has earned 10 points

Montreal has played 8 games and has earned 3 points

Toronto is in first place by 7 points

更新 :

我的代码:

else if (option == 2){

int counter = 0;

int totalpoints = 0;

String phrase = keyboard.next();

while(go){

String letter = keyboard.next();

if (letter.equals("W")){

pointsW++;

}

else if (letter.equals("L")){

pointsL++;

}

else if (letter.equals("O")){

pointsO++;

}

counter++;

if (letter.equals("N")){

totalpoints = pointsW + pointsL + pointsO;

counter--;

go = false;

}

}

int counter2 = 0;

int totalpoints2 = 0;

pointsW = 2;

pointsL = 0;

pointsO = 1;

String phrase2 = keyboard.next();

while (go2){

String letter2 = keyboard.next();

if (letter2.equals("W")){

pointsW++;

}

else if (letter2.equals("L")){

pointsL++;

}

else if (letter2.equals("O")){

pointsO++;

}

counter2++;

if (letter2.equals("Q")){

counter2--;

totalpoints2 = pointsW + pointsL + pointsO;

go2 = false;

}

}

System.out.println(phrase + " has played "+counter+" games and has earned "+totalpoints+" points");

System.out.println(phrase2 + " has played "+counter2+" games and has earned "+totalpoints2+" points");

if (totalpoints > totalpoints2){

System.out.println(phrase + " is in first place by "+(totalpoints - totalpoints2) + " points");

}else{

System.out.println(phrase2 + " is in first place by "+(totalpoints2 - totalpoints) + " points");

}

}

输入样例:

2

Toronto

W

W

L

O

W

O

W

N

Montreal

L

L

O

L

L

W

L

L

Q

问题 :这是我得到的输出“蒙特利尔参加了8场比赛并获得11分”,而应该是“蒙特利尔参加了8场比赛并获得3分”

最后

以上就是忧伤萝莉为你收集整理的java while 定义变量_在while循环中声明字符串变量,而不会在整个循环中循环-Java...的全部内容,希望文章能够帮你解决java while 定义变量_在while循环中声明字符串变量,而不会在整个循环中循环-Java...所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部