java入门习题及答案——编程
1.身高计算尺寸与标准单位之间的转换
要求:输入一个小数,输出一个小数 如输入:5 7 输出 foot 5 inch 7 170.18cm
package hello;
import java.util.Scanner;
public class Transform {
public static void main(String[] args) {
int foot;//尺
double inch;//英寸
Scanner in = new Scanner(System.in);
foot = in.nextInt();
inch = in.nextDouble();
System.out.println("foot "+foot+",inch "+inch);
System.out.println(((foot+inch/12)*0.3048*100)+"cm");
}
}
2.温度转换 华摄氏度转换成摄氏度 如输入100 输出100F=37C
package hello;
import java.util.Scanner;
public class Transform {
public static void main(String[] args) {
int C;
int F;
System.out.println("请输入华摄氏度:");
Scanner in=new Scanner(System .in);
F=in.nextInt();
System.out.println(F+"F ="+(F-32)*5/9+"C");
}
}
3.成绩及格与否判断
package hello;
import java.util.Scanner;
public class Transform {
public static void main(String[] args) {
Scanner in=new Scanner(System .in);
System.out.println("请输入成绩:");
int amount = in.nextInt();
System.out.println("你的成绩是:"+amount);
if(amount>=60)
System.out.println("恭喜你及格了!");
else
System.out.println("没有及格,要继续努力!");
}
}
4.从三个数中出最大的一个数
package hello;
import java.util.Scanner;
public class Transform {
public static void main(String[] args) {
Scanner in=new Scanner(System .in);
int x,y,z;
System.out.println("请输三个数:");
x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
int max = 0;
if(x>y) {
if(x>z) {
max = x;
}
else
max = z;
}
else
if(y>z) {
max = y;
}
else
max =z;
System.out.println("这三个数中最大的是:"+max);
}
}
5.数数几位数
package hello;
import java.util.Scanner;
public class Transform {
public static void main(String[] args) {
System.out.println("请输入一个大于0或者等于0的整数:");
Scanner in=new Scanner(System .in);
int number = in.nextInt();
int count = 0;
do {
number=number/10;
count=count+1;
}while(number>0);
System.out.println("您输入的这个数是:"+count+"位数");
}
}
6.算平均数 (直到输入-1结束)
package hello;
import java.util.Scanner;
public class Transform {
public static void main(String[] args) {
System.out.println("请输入一个大于0或者等于0的整数:");
Scanner in=new Scanner(System .in);
int number =in.nextInt();
int count = 0;
int sum = 0;
while(number!=-1)
{
sum=sum+number;
count=count+1;
number = in.nextInt();
}
if(count>0)
System.out.println("平均数="+(double)sum/count);
}
}
7.阶乘问题 即输入一个数输出它的阶乘
package hello;
import java.util.Scanner;
public class Transform {
public static品质好的java入门编程 void main(String[] args) {
System.out.println("请输入一个数n:");
Scanner in=new Scanner(System .in);
int n =in.nextInt();
int i = 1;
int foctor = 1;
for(i=1;i<=n;i++)
{
foctor=foctor*i;
}
System.out.println("n的阶乘为"+foctor);
}
}
8.输出100以内的所有素数
package hello;
public class Transform {
public static void main(String[] args) {
System.out.println("100以内的素数有:");
for(int n =2;n<100;n++) {
int isPrime = 1;
for(int i=2;i<n;i++) {
if(n%i==0) {
isPrime = 0;
break;
}
}
if(isPrime==1)
System.out.print(n+" ");
}
}
}
9.投票统计 (直到输入-1结束输出结果)
package hello;
import java.util.Scanner;
public class Transform {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x=in.nextInt();
int[]numbers = new int [10];
while(x!=-1) {
if(x>=0&&x<=9) {
numbers[x]++;
}
x = in.nextInt();
}
for(int i=0;i<numbers.length;i++)
System.out.println(i+":"+numbers[i]);
}
}
10.构造素数表
package hello;
import java.util.Scanner;
public class Transform {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
boolean[]isPrime = new boolean [100];
for(int i=0;i<isPrime.length;i++) {
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论