亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

以下代碼是關于java switch和if else問題,麻煩大佬幫忙看看~

以下代碼是關于java switch和if else問題,麻煩大佬幫忙看看~

森欄 2022-01-07 18:07:01
import java.util.Scanner;public class guess{public static void main(String[] args){Scanner in=new Scanner(System.in);System.out.println("猜拳游戲:1是剪刀,2是石頭,3是布");System.out.println("請出拳");int person=in.nextInt();int computer=(int)(Math.random()*3+1);String mark="拳頭";String mark2="拳頭";switch(person){case 1:mark="剪刀";break;case 2:mark="石頭";break;case 3:mark="布";break;}switch(computer){case 1:mark2="剪刀";break;case 2:mark2="石頭";break;case 3:mark2="布";break;}if(person>3){System.out.println("能不能好好玩");}else{if(person==computer){System.out.println("平局");}else if(person==(computer+1)%3+1){//(person==1&&computer==2||person==2&&computer==3||person==3&&computer==1)System.out.println("你出的是:"+mark+" "+"電腦出的是:"+mark2+"-_-你輸了");}else{System.out.println("你出的是:"+mark+" "+"電腦出的是:"+mark2+"~—~你贏了");}}}}能不能將if(person>3){System.out.println("能不能好好玩");}else這句 加載到switch下的 案例case中用switch下 break跳出命令代替if_elseps:本人新手 自學java第3天 望高手能用簡單的語法回答
查看完整描述

2 回答

?
森林海

TA貢獻2011條經驗 獲得超2個贊

import java.util.Scanner;
public class guess{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
System.out.println("猜拳游戲:1是剪刀,2是石頭,3是布");
System.out.println("請出拳");
int person=in.nextInt();
int computer=(int)(Math.random()*3+1);
String mark="拳頭";
String mark2="拳頭";
switch(person){
case 1:
mark="剪刀";
break;
case 2:
mark="石頭";
break;
case 3:
mark="布";
break;

default: //不是1、2、3 說明輸入不對

System.out.println("能不能好好玩");  

return;//跳出方法體,后續都不執行

}


switch(computer){
case 1:
mark2="剪刀";
break;
case 2:
mark2="石頭";
break;
case 3:
mark2="布";
break;

}
if(person>3){
System.out.println("能不能好好玩");

}else{
if(person==computer){
System.out.println("平局");
}else if(person==(computer+1)%3+1){
//(person==1&&computer==2||person==2&&computer==3||person==3&&computer==1)
System.out.println("你出的是:"+mark+" "+"電腦出的是:"+mark2+"-_-你輸了");
}else{
System.out.println("你出的是:"+mark+" "+"電腦出的是:"+mark2+"~—~你贏了");
}}
}
}



查看完整回答
反對 回復 2022-01-10
?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

不能轉換
switch 只支持char int boolean byte類型
不支持條件語句

查看完整回答
反對 回復 2022-01-10
  • 2 回答
  • 0 關注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號