初学JAVA,不知道怎么样练习编程风格,求指导,这个是我最近写的作业"猜数字游戏"
初学JAVA,不知道怎么样练习编程风格,求指导,这个是我最近写的作业"猜数字游戏"各位大哥大姐,帮帮我给我提提建议吧,我知道风格有问题,但不知道从哪里下手.
不介意的话帮我看看程序.挑挑毛病.
新建 好压 7Z 压缩文件.rar
(1.07 KB)
程序代码:
import java.util.Random; import java.util.Scanner; import javax.swing.JOptionPane; public class GuessNumberGame { public static void main(String[] arg) { boolean Flag1 = true; int Success = 0, Failure = 0; String output2="欢迎来到*猜数字*游戏\n在每局游戏中您有3次机会猜猜数字,加油加油"; JOptionPane.showMessageDialog(null,output2); //System.out.println("欢迎来到猜数字游戏"); //System.out.print("在每局游戏中您有3次机会猜数字,好好加油哦,"); while (Flag1) { Random r = new Random(); int getSystem = r.nextInt(10); output2="现在请您从数字0-9选择一个数字输入"; JOptionPane.showMessageDialog(null,output2); //System.out.print("现在请您输入数字0-9:"); int getFromUser = 0; Scanner input = new Scanner(System.in); for (int i = 1; i <= 3; i++) { getFromUser = input.nextInt(); if (getFromUser < getSystem) { output2="您选择的数字太小了,"; JOptionPane.showMessageDialog(null,output2); //System.out.print("您猜的数字太小了,"); if (i == 1) { System.out.print("您还有两次机会!加油!加油!请重新输入数字(0-9):"); } if (i == 2) { System.out.print("您还有一次机会!好好把握呀!请重新输入数字(0-9):"); } } else if (getFromUser > getSystem) { System.out.println("您猜的数字太大了!"); if (i == 1) { System.out.print("您还有两次机会!加油!加油!请重新输入数字(0-9):"); } if (i == 2) { System.out.print("您还有一次机会!好好把握呀!请重新输入数字(0-9):"); } } else { System.out.println("恭喜您,猜对了!"); break; } } if (getFromUser == getSystem) { Success++; } else { System.out.println("正确的答案是" + getSystem); Failure++; } System.out.print("想要继续吗?输入1继续猜数字,输入0退出:"); int Flag2 = input.nextInt(); Flag2 = (int) Flag2; if (Flag2 == 1) { Flag1 = true; } else if (Flag2 == 0) { Flag1 = false; } else { System.out.println("input error"); Flag1 = true; } } System.out.println("您总共答了" + (Success + Failure) + "次,"); System.out.println("其中有" + Failure + "次错误,有" + Success + "次正确."); if (Success > Failure) { String output = "恭喜,您赢了!"; JOptionPane.showMessageDialog(null, output); } else { String output = "很遗憾,您输了!下次幸运!"; JOptionPane.showMessageDialog(null, output); } } }