java练习
课堂练习题目,随机输入一个简单的四则运算,符号也随机,不输出结果。
代码:
1 public static void main(String[] args) { 2 // TODO Auto-generated method stub 3 String[] number = {"0","1","2","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29"}; 4 String[] fuhao = {"+","-","*","/"}; 5 6 int oneLength = number.length; 7 int twoLength = fuhao.length; 8 int threeLength = number.length; 9 10 int rand1 = (int) (Math.random() * oneLength); 11 int rand2 = (int) (Math.random() * twoLength); 12 int rand3 = (int) (Math.random() * threeLength); 13 14 String phrase = number[rand1] + " " +fuhao[rand2] + " " + number[rand3]; 15 16 System.out.println("四则运算试为 " + phrase + " = "); 17 }
数字是手动输入的,因此对代码做改进:
1 public static void main(String[] args) { 2 // TODO Auto-generated method stub 3 String[] fuhao = {"+","-","*","/"}; 4 5 int twoLength = fuhao.length; 6 7 int rand1 = (int) (Math.random() * 99); 8 int rand2 = (int) (Math.random() * twoLength); 9 int rand3 = (int) (Math.random() * 99); 10 11 String phrase = rand1 + " " +fuhao[rand2] + " " + rand3; 12 13 System.out.println("四则运算试为 " + phrase + " = "); 14 }
总结:java编程实践过少
时间: 2024-10-22 11:37:18