选择判断与循环语句的例题:
一、判读语句
1、解一元二次方程
代码:
import java.util.Scanner;
class Class06{
/*
数据:
步骤:
1、提示输入
2、判断a是否等于0
3、delt分类
4、输出结果
*/
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a,b,c:");
double a =scanner.nextDouble();
double b =scanner.nextDouble();
double c =scanner.nextDouble();
double delt=b*b-4*a*c;
if(a==0){
if(b==0){
System.out.println("The equation has no meaning");
}else{
double x=-c/b;
System.out.println("The equation has one root "+x);
}
}else{
//double delt=b*b-4*a*c;
}
if(delt>0){
double x1=(-b+Math.pow(delt,0.5))/2*a;
double x2=(-b-Math.pow(delt,0.5))/2*a;
System.out.println("The equation have two roots "+x1+x2);
}else if(delt==0){
double x=(-b+Math.pow(delt,0.5))/2*a;
System.out.println("The equation has one root "+x);
}else{
System.out.println("The equation has on real roots");
}
}
}
import java.util.Scanner;
class Class07{
/**
步骤:
1、提示输入三位数
2、abc拆开
3、合成新的cba
4、比较
*/
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("Enter a three-digit integer:");
int num=scanner.nextInt();
int temp=num;
//123
int a=temp%10;//3
temp/=10;//12
int b=temp%10;//2
int c=temp/10;//1
//321
int num2=a*100+b*10+c;
if(num==num2){
System.out.println(num+" is a palindrome");
}else{
System.out.println(num+" is not a palindrome");
}
}
}
import java.util.*;
class Class08{
/**
步骤:
1、随机生成一个数com
2、提示用户输入usr
3、判断谁赢
delt=usr-com
delt=0;平局
delt=1,,-2 用户赢
否则电脑赢
4、输出结果
*/
public static void main(String[] args){
String[] c={
"scissor","rock","paper"};
Random random =new Random();
int com=random.nextInt(3);
Scanner scanner=new Scanner(System.in);
System.out.print("scissor(0),rock(1),paper(2) :");
int usr=scanner.nextInt();
int delt=usr-com;
if(delt==0){
System.out.println("The computer is "+c[com]+". You are "+c[usr]+". It is a draw");
}else if(delt==1||delt==-2){
System.out.println("The computer is "+c[com]+". You are "+c[usr]+". You are won");
}else{
System.out.println("The computer is "+c[com]+". You are "+c[usr]+". You are loser");
}
}
}
import java.util.*;
class Class12{
/**
步骤:
1、随机产生数0-100 com
2、提示用户输入数字 usr
3、分类讨论
如果usr=com,输出猜中了
如果usr>com,输出太高了,usr
public static void main(String[] args){
Random random=new Random();
int com=random.nextInt(101);
Scanner scanner=new Scanner(System.in);
System.out.println("Guess a magic number between 0 and 100");
System.out.print("Enter your guess: ");
int usr=scanner.nextInt();
if(usr==com){
System.out.print("Yes,the number is "+com);
}else{
while(true){
if(usr>com){
System.out.println("Your guess is too high");
}else{
System.out.println("Your guess is too low");
}
System.out.print("Enter your guess: ");
usr=scanner.nextInt();
if(usr==com){
System.out.print("Yes,the number is "+com);
break;}
}
}
}
}
import java.util.Scanner;
class Class13{
/**
步骤:
1、提示输入两个整数
2、int gcd=2;
3、判断
*/
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("输入两个整数:");
int n1=scanner.nextInt();
int n2=scanner.nextInt();
int gcd=1;
for(int i=1;i<=(n1<n2?n1:n2);i++){
if(n1%i==0&&n2%i==0){
gcd=i;
}
}
System.out.println("最大公约数为:"+gcd);
}
}
3、有趣的打印
打印*的几种情况,找行数i、*数j、空格k的关系、规律得出
在这里插入代码片class Demo01{
public static void main(String[] args){
/*
****
****
****
****
*/
for(int i=1;i<=4;i++){
System.out.println("****");
}
for(int i=1;i<=4;i++){
for(int j=1;j<=4;j++){
System.out.print("*");
}
System.out.println();
}
/*
*
**
***
****
*/
//i=1~4
for(int i=1;i<=4;i++){
for(int j=1;j<=i;j++){
System.out.print("*");
}
System.out.println();
}
/*
* 1 1
** 2 2
*** 3 3
**** 4 4
*** 5 3
** 6 2
* 7 1
*/ //后三行行数和*个数的关系:i+j=8
for(int i=1;i<=7;i++){
for(int j=1;j<=i&&j<=8-i;j++){
System.out.print("*");
}
System.out.println();
}
/* i k(4-i) j
* 1 3 1
** 2 2 2
*** 3 1 3
**** 4 0 4
*** 5 1 3
** 6 2 2
* 7 3 1
*///k表示空格数(后三行结果是负的所以取绝对值)
for(int i=1;i<=7;i++){
for(int k=1;k<=Math.abs(i-4);k++){
System.out.print(" ");
}
for(int j=1;j<=i&&j<=8-i;j++){
System.out.print("*");
}
System.out.println();
}
/* i
* 1
* * 2
* * * 3
* * * * 4
* * * 5
* * 6
* 7
*/
for(int i=1;i<=7;i++){
for(int j=1;j<=Math.abs(4-i);j++){
System.out.print(" ");
}
for(int j=1;j<=i&&j<=8-i;j++){
System.out.print("* ");
}
System.out.println();
}
/* i
* 1
* * 1 2
* * 1 2 3
* * 1 2 3 4
* * 1 2 3
* * 1 2
* 1
j==1 ||i==j||j<=8-i
*/
for(int i=1;i<=7;i++){
for(int k=1;k<=Math.abs(4-i);k++){
System.out.print(" ");
}
for(int j=1;j<=i&&j<=8-i;j++){
if(j==1 ||j==i||j==8-i){
System.out.print("* ");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}