zoukankan      html  css  js  c++  java
  • 简单四则运算的随机输入

    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编程实践过少

  • 相关阅读:
    hdu 3342 Legal or Not 拓排序
    hdu 1596 find the safest road Dijkstra
    hdu 1874 畅通工程续 Dijkstra
    poj 2676 sudoku dfs
    poj 2251 BFS
    poj Prime Path BFS
    poj 3278 BFS
    poj 2387 Dijkstra 模板
    poj 3083 DFS 和BFS
    poj 1062 昂贵的聘礼 dijkstra
  • 原文地址:https://www.cnblogs.com/SanShaoS/p/4319678.html
Copyright © 2011-2022 走看看