文档详情

JAVA编程题--答案..doc

发布:2017-01-05约6.53千字共6页下载文档
文本预览下载声明
1.编写一个输出Hello World!的程序。 参考答案1、 public class HelloWorld { ?public void sayHello() ?? { ? ? ? ? ? ? System.out.println(“HelloWorld!”) ? ? } public static void main(String args[]) ?? { ? ? ? ? ? ? HelloWorld hello=new HelloWorld(); ? ? hello. sayHello } } } 2.利用if语句,根据下列函数编写一个程序,当键盘输入x值时,求出并输出y的值。 参考答案1、 import java.io.*; public class X3_4_1 { public static void main(String[] args) throws IOException{ InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); int x, y; x = Integer.parseInt(br.readLine()); if(x=1) y = x; else if(x10) y = 3*x - 2; else y=4*x; System.out.println(x = +x + \ty = +y); } } 参考答案2、 import java.util.Scanner; public class Test1 { public static void main(String[] args) { Scanner read=new Scanner(System.in); int x, y; x = read.nextInt(); if(x=1) y = x; else if(x10) y = 3*x - 2; else y=4*x; System.out.println(x = +x + \ty = +y); } }                        3、编写程序,输出1到1000之间所有可以被3整除又可以被7整除的数。 public class Exercises{ public static void main(String[] args) { int n = 1, i; for (i = 1; i = 1000; i ++) {if ((i%3==0)(i%7==0)) System.out.print(i + ); } } } 4、求出100以内的素数,其中最小的素数是2。 public class Exercises{ public static void main(String[] args) { int n = 1, m, j, i; for (i = 2; i = 100; i ++) { m = (int) Math.sqrt((double) i); for (j = 2; j = m; j++) if ((i %
显示全部
相似文档