1、循环
import java.util.Scanner;
public class Hello {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int balance = 0;
while (true){
System.out.print("请投币:");
int amount = scan.nextInt();
balance = balance + amount;
if (balance >= 10){
System.out.println("********************");
System.out.println("********Java城际铁轨***");
System.out.println("*****票价10元*********");
System.out.println("*********************");
System.out.println("找零:"+(balance-10));
balance = 0;
}
}
}
}
2、数数字
import java.util.Scanner;
public class Hello {
public static void main(String[] args){
//判断一个数字的位数,0和负数除外
int count = 0;
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
while (number>0){
number = number / 10;
count = count + 1;
}
System.out.println(count);
}
}
3、do-while循环
import java.util.Scanner;
public class Hello {
public static void main(String[] args){
//判断一个数字的位数,包含0,不包含负数
int count = 0;
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
do {
number = number / 10;
count = count + 1;
}while (number > 0);
System.out.println(count);
}
}
4、算平均数
import java.util.Scanner;
public class Hello {
public static void main(String[] args){
//计算输入数字的平均数,输入-1时结束程序
Scanner in = new Scanner(System.in);
int number = in.nextInt();
int sum = 0;
int count = 0;
while (number != -1){
sum = sum + number;
count = count + 1;
number = in.nextInt();
}
if (count > 0) {
System.out.println("平均数为:" + (double) sum / count);
}
}
}
import java.util.Scanner;
public class Hello {
public static void main(String[] args){
//计算输入数字的平均数,输入-1时结束程序
Scanner in = new Scanner(System.in);
int number;
int sum = 0;
int count = 0;
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);
}
}
}
5、猜数游戏
import java.util.Scanner;
public class Hello {
public static void main(String[] args){
//猜数,Math.random() 产生一个【0,1)之间的浮点数
int num = (int)(Math.random()*100 + 1);//[0,1)---[0,100)---[0,101)---[1,100]
int a ;
int count = 0;
Scanner in = new Scanner(System.in);
do {
a = in.nextInt();
count = count + 1;
if (a > num) {
System.out.println("偏大");
} else if (a < num) {
System.out.println("偏小");
}
}while(a != num);
System.out.println("猜对了!共猜了"+ count+"次");
}
}
import java.util.Scanner;
public class Hello {
public static void main(String[] args){
//猜数,Math.random() 产生一个【0,1)之间的浮点数
int number = (int)(Math.random()*100 + 1);//[0,1)---[0,100)---[0,101)---[1,100]
int a ;
int count = 0;
Scanner in = new Scanner(System.in);
a = in.nextInt();
count = count + 1;
while (a != number){
if (a>number){
System.out.println("偏大");
}else {
System.out.println("偏小");
}
a = in.nextInt();
count = count + 1;
}
System.out.println("猜对了!共猜了"+count+"次");
}
}
6、整数分解
import java.util.Scanner;
//将整数逆序输出
public class Hello {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("请输入一位整数:");
int number = in.nextInt();
int result = 0;
do {
int digit = number % 10;
result = 10 * result + digit;
number = number / 10;
}while(number > 0);
System.out.println(result);
}
}
7、阶乘
import java.util.Scanner;
public class Hello {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int factor = 1;
int i = 1;
while (i <= n){
factor = factor * i;
i = i + 1;
}
System.out.println(factor);
}
}
8、素数
import java.util.Scanner;
public class Hello {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int isPrime = 1;
int n = in.nextInt();
for (int i = 2; i < n; i++){
if(n%i == 0){
isPrime = 0;
System.out.println(n+"不是素数,i="+i);
break;
}
}
if(isPrime==1){
System.out.println(n+"是素数");
}else{
System.out.println(n+"不是素数");
}
}
}
9、100以内的素数
import java.util.Scanner;
public class Hello {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
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+" ");
}
}
}
}
10、九九乘法表
public class Hello {
public static void main(String[] args){
for(int i=1; i<=9; i++){
for(int j=1; j<=i; j++){
System.out.print(j+"*"+i+"="+i*j+"\t");
}
System.out.println();
}
}
}
11、输出前50个素数
public class Hello{
public static void main(String[] args){
int add = 0;
int n = 2;
while(add!=50){
int isPrime = 1;
for(int i = 2; i<n; i++){
if (n%i == 0){
isPrime = 0;
break;
}
}
if(isPrime == 1) {
System.out.print(n + " ");
add++;
}
n++;
}
}
}
12、 凑硬币
import java.util.Scanner;
public class Hello{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int amount = in.nextInt();
OUT:
for(int one=0; one <=amount; one++){
for(int five=0; five <= amount/5; five++){
for(int ten=0; ten <= amount/10; ten++){
for(int twenty=0; twenty <= amount/20; twenty++){
if(one+five*5+ten*10+twenty*20==amount){
System.out.println(one+"张一元,"+five+"张五元,"+ten+"张十元,"+twenty+"张二十元。");
break OUT;
}
}
}
}
}
}
}
标签:count,Java,入门,int,练习,System,number,public,Scanner From: https://www.cnblogs.com/feimanzhh/p/16930842.html