请各位指点一下 我的目的,是让各位帮我看看我写的对吗?因为我觉得只能输入0-2三个数,但是我如果输入3,计算机总是显示LOSE,我并没有在程序里输入0,1,2为什么还是能比较大小呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 int 类型系统初始化为0,即使你没输也有值为0 我如何指出A>B, B>C, C> A 呢 你好,我现在有个地方比较糊涂。因为,我想让 数字‘1’代表石头(即ROCK = 1,‘2’代表布 ,剪子 为 0。 比较的时候计算机是以数字大小为依据比较的,可是代表剪子的‘0’怎么也不能比代表布的‘2’大呀。怎么能解决这个问题呢?谢谢 我在jdk1.4下除了不输入有出错外,都没有什么问题啊,输入3(4,5,...,n)也可以啊,-1(-2,-3)都可以.import java.util.*;import javax.swing.*;public class game{ public static void main(String[] args){ //Comment on what variable are int OurGuess, ComputerChoice; int a, b, c; //System.out.println("OurGuess:"+OurGuess); String OurGuess_str; OurGuess_str = JOptionPane.showInputDialog("rock=0\n\n" + "csissor=1 " + "\n\n" + "paper=2 " + "\n\n"+"your guess"); OurGuess = Integer.parseInt(OurGuess_str); System.out.println("OurGuess:"+OurGuess); a = Integer.parseInt(OurGuess_str); System.out.println("A:"+a); b = Integer.parseInt(OurGuess_str); System.out.println("B:"+b); c = Integer.parseInt(OurGuess_str); System.out.println("C:"+c); Random generator = new Random(); ComputerChoice = generator.nextInt(3); System.out.println("ComputerChoice:"+ComputerChoice); if(OurGuess > ComputerChoice) System.out.println("win "); else System.out.println("lose "); while (ComputerChoice != OurGuess){ System.exit(0); } }} 你可以自己写判断啊比如说:if( OurGuess == 0 && ComputerChoice == 2 ) OurGuess += 3;if( OurGuess == 2 && ComputerChoice == 0 ) ComputerChoice += 3;if(OurGuess > ComputerChoice) System.out.println("win ");else System.out.println("lose "); 谢谢 iamqqmyheart(注销||登录) 可是,我发现计算机还是按数的大小来判断的,例如:我输入‘1’=剪子,计算机的数是‘0‘=石头,可是计算机说我WIN我现在有个地方比较糊涂。因为,我想让 数字‘1’代表石头(即ROCK = 1,‘2’代表布 ,剪子 为 0。 比较的时候计算机是以数字大小为依据比较的,可是代表剪子的‘0’怎么也不能比代表布的‘2’大呀。怎么能解决这个问题呢?谢谢 想法不对而且没遵守命名规则import java.awt.event.*;import javax.swing.*;import java.util.*;public class RockPaperScissor{ public static void main(String[] args) { new RockPaperScissor().new Game(); } class Game extends JFrame { String userChoice; String computerChoice; Game() { super("Game"); setup(); } void setup() { final String[] options = { "Rock", "Paper", "Scissor" }; JComboBox optionList = new JComboBox(options); optionList.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox) e.getSource(); userChoice = (String) cb.getSelectedItem(); int index = new Random().nextInt(3); computerChoice = options[index]; if (computerChoice.equals(userChoice)) { JOptionPane.showMessageDialog( Game.this, "Dogfall \n\n" + "user choice = " + userChoice + "\n\n" + "computer choice = " + computerChoice); return; } if (computerChoice.equals(options[0])) { if (userChoice.equals(options[1])) { JOptionPane.showMessageDialog( Game.this, "Win \n\n" + "user choice = " + userChoice + "\n\n" + "computer choice = " + computerChoice); return; } else { JOptionPane.showMessageDialog( Game.this, "Lose \n\n" + "user choice = " + userChoice + "\n\n" + "computer choice = " + computerChoice); return; } } if (computerChoice.equals(options[1])) { if (userChoice.equals(options[2])) { JOptionPane.showMessageDialog( Game.this, "Win \n\n" + "user choice = " + userChoice + "\n\n" + "computer choice = " + computerChoice); return; } else { JOptionPane.showMessageDialog( Game.this, "Lose \n\n" + "user choice = " + userChoice + "\n\n" + "computer choice = " + computerChoice); return; } } if (computerChoice.equals(options[2])) { if (userChoice.equals(options[0])) { JOptionPane.showMessageDialog( Game.this, "Win \n\n" + "user choice = " + userChoice + "\n\n" + "computer choice = " + computerChoice); return; } else { JOptionPane.showMessageDialog( Game.this, "Lose \n\n" + "user choice = " + userChoice + "\n\n" + "computer choice = " + computerChoice); return; } } } }); this.getContentPane().add(optionList); this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); this.setBounds(200, 200, 200, 100); this.setResizable(false); this.setVisible(true); } }} //提示楼主,用数字来比较的话,可以用减法userchoice - computerchoice//根据结果判断//程序如下import java.awt.event.*;import javax.swing.*;import java.util.*;public class RockPaperScissor{ public static void main(String[] args) { new RockPaperScissor().new Game(); } class Game extends JFrame { int userChoice; int computerChoice; Game() { super("Game"); setup(); } void setup() { final String[] options = { "Rock", "Paper", "Scissor" }; JComboBox optionList = new JComboBox(options); optionList.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox)e.getSource(); userChoice = cb.getSelectedIndex(); computerChoice = new Random().nextInt(3); int result = userChoice - computerChoice; if (result == 0) { JOptionPane.showMessageDialog( Game.this, "Dogfall \n\n" + "user choice = " + options[userChoice] + "\n\n" + "computer choice = " + options[computerChoice]); return; } else if (result == 1 || result == -2) { JOptionPane.showMessageDialog( Game.this, "Win \n\n" + "user choice = " + options[userChoice] + "\n\n" + "computer choice = " + options[computerChoice]); return; } else if (result == -1 || result == 2) { JOptionPane.showMessageDialog( Game.this, "Lose \n\n" + "user choice = " + options[userChoice] + "\n\n" + "computer choice = " + options[computerChoice]); return; } } }); this.getContentPane().add(optionList); this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); this.setBounds(200, 200, 200, 100); this.setResizable(false); this.setVisible(true); } } 菜鸟刚刚接触JAVA,书本中有个问题不懂,请教大侠们 String 转 Integer 这么小的程序还有异常?? 跪求高手!!!1 谁帮我看下这个程序 编译通过 但是运行出错! 关于menuitem快捷键``` 线程间管道通信 帮我找点东西呀 树型结构刷新后怎么还是变成原来的 网络图片抓下来不完整,打不开,因为此文件已损坏或过大 使用MyEclipse无法创建XML文件(创建File文件) 关于TOMCAT的配置. 为什么我的JAVA程序运行是老出错:Exception in thread "main" java.lang.NoClassDefFoundError:HelloWorld
比较的时候计算机是以数字大小为依据比较的,可是代表剪子的‘0’怎么也不能比代表布的‘2’大呀。怎么能解决这个问题呢?谢谢
import javax.swing.*;public class game{
public static void main(String[] args){ //Comment on what variable are
int OurGuess, ComputerChoice;
int a, b, c; //System.out.println("OurGuess:"+OurGuess);
String OurGuess_str;
OurGuess_str = JOptionPane.showInputDialog("rock=0\n\n" + "csissor=1 " + "\n\n" + "paper=2 " + "\n\n"+"your guess");
OurGuess = Integer.parseInt(OurGuess_str);
System.out.println("OurGuess:"+OurGuess);
a = Integer.parseInt(OurGuess_str);
System.out.println("A:"+a);
b = Integer.parseInt(OurGuess_str);
System.out.println("B:"+b);
c = Integer.parseInt(OurGuess_str);
System.out.println("C:"+c); Random generator = new Random(); ComputerChoice = generator.nextInt(3);
System.out.println("ComputerChoice:"+ComputerChoice); if(OurGuess > ComputerChoice)
System.out.println("win ");
else
System.out.println("lose ");
while (ComputerChoice != OurGuess){
System.exit(0);
}
}
}
比如说:if( OurGuess == 0 && ComputerChoice == 2 )
OurGuess += 3;if( OurGuess == 2 && ComputerChoice == 0 )
ComputerChoice += 3;if(OurGuess > ComputerChoice)
System.out.println("win ");
else
System.out.println("lose ");
可是,我发现计算机还是按数的大小来判断的,例如:我输入‘1’=剪子,计算机的数是‘0‘=石头,可是计算机说我WIN我现在有个地方比较糊涂。因为,我想让 数字‘1’代表石头(即ROCK = 1,‘2’代表布 ,剪子 为 0。
比较的时候计算机是以数字大小为依据比较的,可是代表剪子的‘0’怎么也不能比代表布的‘2’大呀。怎么能解决这个问题呢?谢谢
import javax.swing.*;
import java.util.*;public class RockPaperScissor
{
public static void main(String[] args)
{
new RockPaperScissor().new Game();
} class Game extends JFrame
{
String userChoice;
String computerChoice; Game()
{
super("Game");
setup();
} void setup()
{
final String[] options = { "Rock", "Paper", "Scissor" };
JComboBox optionList = new JComboBox(options); optionList.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JComboBox cb = (JComboBox) e.getSource();
userChoice = (String) cb.getSelectedItem(); int index = new Random().nextInt(3);
computerChoice = options[index]; if (computerChoice.equals(userChoice))
{
JOptionPane.showMessageDialog(
Game.this,
"Dogfall \n\n"
+ "user choice = "
+ userChoice
+ "\n\n"
+ "computer choice = "
+ computerChoice);
return;
} if (computerChoice.equals(options[0]))
{
if (userChoice.equals(options[1]))
{
JOptionPane.showMessageDialog(
Game.this,
"Win \n\n"
+ "user choice = "
+ userChoice
+ "\n\n"
+ "computer choice = "
+ computerChoice);
return;
}
else
{
JOptionPane.showMessageDialog(
Game.this,
"Lose \n\n"
+ "user choice = "
+ userChoice
+ "\n\n"
+ "computer choice = "
+ computerChoice);
return;
}
}
if (computerChoice.equals(options[1]))
{
if (userChoice.equals(options[2]))
{
JOptionPane.showMessageDialog(
Game.this,
"Win \n\n"
+ "user choice = "
+ userChoice
+ "\n\n"
+ "computer choice = "
+ computerChoice);
return;
}
else
{
JOptionPane.showMessageDialog(
Game.this,
"Lose \n\n"
+ "user choice = "
+ userChoice
+ "\n\n"
+ "computer choice = "
+ computerChoice);
return;
}
}
if (computerChoice.equals(options[2]))
{
if (userChoice.equals(options[0]))
{
JOptionPane.showMessageDialog(
Game.this,
"Win \n\n"
+ "user choice = "
+ userChoice
+ "\n\n"
+ "computer choice = "
+ computerChoice);
return;
}
else
{
JOptionPane.showMessageDialog(
Game.this,
"Lose \n\n"
+ "user choice = "
+ userChoice
+ "\n\n"
+ "computer choice = "
+ computerChoice);
return;
}
}
}
}); this.getContentPane().add(optionList); this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setBounds(200, 200, 200, 100);
this.setResizable(false);
this.setVisible(true);
}
}
}
//根据结果判断
//程序如下
import java.awt.event.*;
import javax.swing.*;
import java.util.*;public class RockPaperScissor
{
public static void main(String[] args)
{
new RockPaperScissor().new Game();
} class Game extends JFrame
{
int userChoice;
int computerChoice; Game()
{
super("Game");
setup();
} void setup()
{
final String[] options = { "Rock", "Paper", "Scissor" };
JComboBox optionList = new JComboBox(options); optionList.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JComboBox cb = (JComboBox)e.getSource();
userChoice = cb.getSelectedIndex(); computerChoice = new Random().nextInt(3); int result = userChoice - computerChoice;
if (result == 0)
{
JOptionPane.showMessageDialog(
Game.this,
"Dogfall \n\n"
+ "user choice = "
+ options[userChoice]
+ "\n\n"
+ "computer choice = "
+ options[computerChoice]);
return;
}
else if (result == 1 || result == -2)
{
JOptionPane.showMessageDialog(
Game.this,
"Win \n\n"
+ "user choice = "
+ options[userChoice]
+ "\n\n"
+ "computer choice = "
+ options[computerChoice]);
return;
}
else if (result == -1 || result == 2)
{
JOptionPane.showMessageDialog(
Game.this,
"Lose \n\n"
+ "user choice = "
+ options[userChoice]
+ "\n\n"
+ "computer choice = "
+ options[computerChoice]);
return;
}
}
}); this.getContentPane().add(optionList); this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.setBounds(200, 200, 200, 100);
this.setResizable(false);
this.setVisible(true);
}
}