- package project.select;
- import java.util.Arrays;
- import javax.swing.JOptionPane;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- public class SelectLeader {
- public static void main(String[] args) {
- final String ino="1号:修罗;2号:地狱犬;3号:啸月天狼;4号:神秘人\n";
- final String rule="出拳规则:1.石头;2.剪刀;3.布 \n" +
- "请出拳";
- String[] candidate={"修罗","地狱犬","啸月天狼","神秘人"};
- //第一步:输入
- int[] students=new int[40];
- String input="";
- int sno=0;
- String prompt="";
- int count = 0;
- for(int i=0;i<3;i++){
- prompt="----------------欢迎进入游戏世界---------------\n";
- input=JOptionPane.showInputDialog(prompt+ino+rule);
- if(input!=null){//表明点击了取消键,表示放弃,此时数组元素值是0
- /* while循环处理用户输入的非有效数组的情况
- * (1)给出提示信息
- * (2)让用户重新输入*/
- while(!input.equals("1")&
- !input.equals("2")&
- !input.equals("3")){
- //给用户提示信息
- JOptionPane.showMessageDialog(null, "请输入有效数字!");
- //让用户重新输入
- input=JOptionPane.showInputDialog(prompt+ino+rule);
- //如果用户重新输入时,选择放弃,则通过break语句跳出while循环
- if(input==null){
- break;
- }
- }
- //由于while循环中,有break语句,所以此处要有条件判断
- if(input!=null){
- sno=Integer.parseInt(input);
- students=sno;
- }
- }
- }
- int n=random();
- int n1=Integer.parseInt(input);
- count=result11(n,n1,count);
- String result="游戏结果";
- result+="分为:"+count+"\n";
- if(count>=2){
- result+="您赢了";
- }else{
- result+= "您输了";
- }
- JTextArea jta=new JTextArea(20,30);//文本区对象
- jta.setText(result);//文本区对象的显示内容
- JScrollPane jsclp=new JScrollPane(jta);//给文本区添加滚动条
- JOptionPane.showMessageDialog(null, jsclp);
- System.out.println(count);
- }
- public static int result11(int n,int m,int count){
- if(n == m){
- System.out.println("平了");
- }else{
- if(m==1){
- if(n==2){
- JOptionPane.showMessageDialog(null,"你输了");
- }else if(n ==3){
- JOptionPane.showMessageDialog(null,"你赢了");
- count++;
- }
- }else if(m == 2) {
- if(n == 1){
- JOptionPane.showMessageDialog(null,"你赢了");
- count++;
- }else if(n ==3){
- JOptionPane.showMessageDialog(null,"你输了");
- }
- }else if( m ==3 ){
- if(n==1){
- JOptionPane.showMessageDialog(null,"你输了");
- }else if(n == 2){
- JOptionPane.showMessageDialog(null,"你赢了");
- count++;
- }
- }
- }
- return count;
- }
- public static int random() {
- int h = (int)(Math.random()*3+1);
- return h;
- }
- }代码题
复制代码 |