一些小程序的代码
Posted 大神笨蛋
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一些小程序的代码相关的知识,希望对你有一定的参考价值。
自动生成随机数的代码:
import javax.swing.JOptionPane; public class RandomInt { public static void main( String args[] ) { int value; String output = ""; for ( int i = 1; i <= 1000; i++ ) { value = 1 + (int) ( Math.random() * 1000 ); output += value + " "; if ( i % 5 == 0 ) output += "\\n"; } JOptionPane.showMessageDialog( null, output,//通过窗口显示出来 "20 Random Numbers from 1 to 1000", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } }
二:关于参数类型的程序
// MethodOverload.java // Using overloaded methods public class MethodOverload { public static void main(String[] args) { System.out.println("The square of integer 7 is " + square(7)); System.out.println("\\nThe square of double 7.5 is " + square(7.5)); } public static int square(int x) { return x * x; } public static double square(double y) { return y * y; } }
可以看出参数的顺序和类型不一样出来的结果也是不一样的。
以上是关于一些小程序的代码的主要内容,如果未能解决你的问题,请参考以下文章
Android课程---Android Studio使用小技巧:提取方法代码片段