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分”

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值