Discuss / Java / 我的三目运算第一个判断平局的等号和最后一个flat赋值一直报错

我的三目运算第一个判断平局的等号和最后一个flat赋值一直报错

Topic source

package com.itranswarp.learnjava;

import java.util.Scanner;

/**

 * switch实现石头/剪子/布并判断胜负

 */

public class Main {

public static void main(String[] args) {

System.out.println("please choice:");

System.out.println(" 1: Rock");

System.out.println(" 2: Scissors");

System.out.println(" 3: Paper");

// 用户输入:

Scanner scanner=new Scanner(System.in);

int choice =scanner.nextInt();

// 计算机随机数 1, 2, 3:

int random = 1 + (int) Math.random() * 3;

String flat;

switch (choice) {

case 1:

random==1?flat="tie":(random==2?flat="win":flat="lose");

break;

case 2:

random==2?flat="tie":(random==3?flat="win":flat="lose");

break;

case 3:

random==3?flat="tie":(random==1?flat="win":flat="lose");

break;

// TODO:

}

System.out.println(flat);

}

}

已经解决了,三目运算不可以直接赋值,(1+(int)Math.random()*3)只能得到1,应该改为(int)(Math.random()*3+1)


  • 1

Reply