P17 3-1 循环-1
package hello;
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int balance=0;
//读入投币金额
while(true)
{
System.out.print("请投币:");
int amount=in.nextInt();
balance=balance+amount;
if (balance>=10)
{
//打印车票
System.out.println("**************");
System.out.println("* Java城际铁路专线 *");
System.out.println("* 无指定座位票 *");
System.out.println("* 票价:10元 *");
System.out.println("**************");
//计算并打印找零
System.out.println("找零:"+(balance-10));
balance=0;
}
}
}
}
P18 3-1 循环-2
package hello;
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int number=in.nextInt();
int count=0;
while(number>0)
{
number=number/10;
count = count+1;
}
System.out.println(count);
}
}
P19 3-1 循环-3
该程序对0不胜任。
P20 3-1 循环-4
package hello;
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
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);
}
}
do{}
while();这个分号不能丢掉
P21 3-1 循环计算-1
P22 3-1 循环计算-2
package hello;
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int number;
int sum=0;
int count=0;
number=in.nextInt();
while(number!=-1)
{
sum=sum+number;
count=count+1;
number=in.nextInt();
}
if(count>0)
{
System.out.println("平均数="+(double)sum/count);
}
}
}
换成do while 循环
package hello;
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int number;
int sum=0;
int count=0;
number=in.nextInt();
do
{
number=in.nextInt();
if(number!=-1)
{
sum=sum+number;
count=count+1;
}
}
while(number!=-1);
if(count>0)
{
System.out.println("平均数="+(double)sum/count);
}
}
}
P23 3-1 循环计算-3
package hello;
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int number=(int)(Math.random()*100+1);//[1,100] Math.random():[0,1)
int a;
int count=0;
do
{
a = in.nextInt();
count=count+1;
if(a>number)
{
System.out.println("偏大");
}
else if(a<number)
{
System.out.println("偏小");
}
}
while(a!=number);
System.out.println("恭喜你,你猜了"+count+"次");
}
}
一定能在7次之内猜到正确的数字。
P24 3-1 循环计算-4
package hello;
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int number;
number=in.nextInt();
do{
int digit=number%10;
System.out.print(digit);
number=number/10;
}while(number>0);
System.out.println();//输出空行
}
}
package hello;
import java.util.Scanner;
public class Hello {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int number;
number=in.nextInt();
int result=0;
do{
int digit=number%10;
result=result*10+digit;
System.out.print(digit);
number=number/10;
}while(number>0);
System.out.println();//输出空行
System.out.println(result);
}
}