5.1
package demo;
import java.util.Scanner;
public class diwuzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer, the input ends if it is 0: ");
int pos = 0;
int neg = 0;
double sum =0;
int amo = 0;
// while(true)是一个无限循环。
while(true){
int tmp = input.nextInt();;
if(tmp==0)
break;
else
{
amo++;
sum+=tmp;
if(tmp>0)
pos++;
else
neg++;
}
}
double ave = sum /amo;
if(amo==0)
System.out.println("No number are entered except 0");
else{
System.out.println("The number of positives is "+pos);
System.out.println("The number of negatives is "+neg);
System.out.println("The total is "+sum);
System.out.println("The average is "+ave);
}
}
}
5.2
package demo;
import java.util.Scanner;
public class diwuzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
final int NUMBER_OF_QUESTIONS=10;
int correctCount = 0;
int count = 0;
long startTime = System.currentTimeMillis();
String output="";
Scanner input = new Scanner(System.in);
while(count<NUMBER_OF_QUESTIONS)
{
int number1 = (int)(Math.random()*15);
int number2 = (int)(Math.random()*15);
System.out.print("What is "+number1+" + "+number2+"? ");
int answer = input.nextInt();
if(number1+number2==answer)
{
System.out.println("You are correct!");
correctCount++;
}
else
System.out.println("Your answer is wrong.\n"+number1+" + "+number2+" should be "+(number1+number2));
count++;
output+="\n"+number1+"+"+number2+"="+answer+((number1+number2==answer)?" correct":" wrong");
}
long endTime = System.currentTimeMillis();
long testTime = endTime-startTime;
System.out.println("Correct count is "+correctCount+"\nTest time is "+testTime/100+" seconds\n"+output);
}
}
5.3
package demo;
import java.util.Scanner;
public class diwuzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("千克 磅");
for(int i=1;i<=100;i++){
System.out.printf("%-6d%8.1f\n", 2*i-1,(2*i-1)*2.2);
}
}
}
5.4
package demo;
import java.util.Scanner;
public class diwuzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("英里 千米");
for(int i = 1;i<=10;i++){
System.out.printf("%-11d%-6.3f\n", i,i*1.609);
}
}
}
5.5
package demo;
import java.util.Scanner;
public class diwuzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("千克 磅 磅 千克");
for(int i=1;i<=100;i++){
System.out.printf("%-6d%8.1f%8d%12.2f\n", 2*i-1,(2*i-1)*2.2,5*i+15,(5*i+15)/2.2);
}
}
}
5.6
package demo;
import java.util.Scanner;
public class diwuzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("英里 千米 千米 英里");
for(int i=1;i<=10;i++)
System.out.printf("%-8d%-7.3f %-8d%-6.3f\n",i,i*1.609,5*i+15,(5*i+15)/1.609);
}
}
5.7
package demo;
import java.util.Scanner;
public class diwuzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
int pre = 10000;
for(int i = 0;i<10;i++)
pre *= (1+0.05);
double pre1 = pre;
double sum = 0;
for(int i = 0 ;i<4;i++){
sum+=pre;
pre *= (1+0.05);
}
System.out.printf("十年后:%.2f\n", pre1);
System.out.printf("总费用的四年后:%.2f\n", sum);
}
}
5.8
package demo;
import java.util.Scanner;
public class diwuzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("一共多少人数: ");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
System.out.println("请着个输入名字和分数: ");
String name = "";
double cup = 0;
for(int i = 0 ; i<num;i++){
String tname = input.next();
double tmp = input.nextDouble();
if(tmp>cup)
{
cup=tmp;
name=tname;
}
}
System.out.println("最高分:"+name);
}
}
5.9
package demo;
import java.util.Scanner;
public class diwuzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("一共多少人数: ");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
System.out.println("请着个输入名字和分数: ");
String name = "";
String name2= "";
double cup = 0;
double cup2 = 0;
for(int i = 0 ; i<num;i++){
String tname = input.next();
double tmp = input.nextDouble();
if(tmp>cup)
{
cup2=cup;
name2=name;
cup=tmp;
name=tname;
}else if(tmp>cup2)
{
cup2=tmp;
name2=tname;
}
}
System.out.println("最高分:"+name);
System.out.println("其次: "+name2);
}
}
5.10
package demo;
import java.util.Scanner;
public class diwuzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
int count=0;
int base = 0;
for(int i=100;i<=1000;i++)
{
if(i%5==0&&i%6==0)
{
count++;
System.out.print(i+" ");
}
if(count%10==0&&count!=base) {
System.out.print("\n");
base+=10;
}
}
}
}
5.11
package demo;
import java.util.Scanner;
public class diwuzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
int count=0;
int base = 0;
for(int i=100;i<=1000;i++)
{ // 使用异或
if(i%5==0^i%6==0)
{
count++;
System.out.print(i+" ");
}
if(count%10==0&&count!=base) {
System.out.print("\n");
base+=10;
}
}
}
}
5.12
package demo;
import java.util.Scanner;
public class diwuzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n=0;
for(int i=0;i*i<12000;i++){
n++;
}
System.out.println(n);
}
}
5.13
package demo;
import java.util.Scanner;
public class diwuzhang {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n=0;
for(int i=0;i*i*i<12000;i++){
n++;
}
System.out.println(n);
}
}