用户交互Scanner
实验
import java.util.Scanner;
public class Dome01 {
public static void main(String[] args) {
Scanner scanner =new Scanner (System.in);
System.out.println("使用Next方式接受");
if(scanner.hasNext()){
String str = scanner.next();
System.out.println("输出的内容为:"+str);
}
}
}
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("使用next line方式接收:");
if(scanner.hasNextLine()){
String str =scanner.nextLine();
System.out.println("输出内容为:"+str);
}
}
}
![]
package control;
import java.util.Scanner;
public class Dome04 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int x = 0;
float f = 0.0f;
System.out.println("请输入整数:");
if (s.hasNextInt()) {
x = s.nextInt();
System.out.println("你输入的整数是:" + x);
} else {
System.out.println("你输入的不是整数");
}
System.out.println("请输入小数:");
if(s.hasNextFloat()){
f= s.nextFloat();
System.out.println("你输入的小数是:"+f);
}else{
System.out.println("你输入的不是小数");
}
s.close();
}
}
package control;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
System.out.println("请依次输入数字:");
Scanner s = new Scanner(System.in);
int x=0;
double sum=0;
int c=0;
while(s.hasNextDouble()){
double a = s.nextDouble();
x=x+1;
sum=sum+a;
System.out.println("你输入的第"+x+"的和为"+sum);
}
c=1+x;
System.out.println("结束原因是你输入的第"+c+"个数不是数字");
System.out.println("你总共输入"+x+"个数的平均值为:"+sum/x);
s.close();
}
}
顺序结构
System.out.println("hello1");//顺序结构
System.out.println("hello2");
System.out.println("hello3");
System.out.println("hello4");
System.out.println("hello5");
System.out.println("hello6");
选择结构
package control;
import java.util.Scanner;
public class ShunZeDemo {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String str=s.nextLine();
if(str.equals("hello")){//"equals":是判断字符串是否相等;
System.out.println(str);
}else{
System.out.println("错误");
}
s.close();
}
}
package control;
import java.util.Scanner;
public class ShunZeDome02 {
public static void main(String[] args) {
//考试分数大于60就及格,小于60就不及格;
Scanner s = new Scanner(System.in);
System.out.println("请输入你的分数:");
int score =s.nextInt();
if(score>60){//score:成绩;
System.out.println("及格");
}else{
System.out.println("不及格");
}
s.close();
}
}
package control;
import java.util.Scanner;
public class ShunZeDemo03 {
public static void main(String[] args) {
//考试满分为100,输出“恭喜你满分”,90~100为A级;80~90为B级;80~70为C级;70~60为D级;0~59为不及格;
Scanner s=new Scanner(System.in);
System.out.println("请输入你的分数:");
int score = s.nextInt();
if(score ==100){
System.out.println("恭喜你满分");
} else if (score <100&&score >90) {
System.out.println("A级");
} else if (score <90&&score >80) {
System.out.println("B级");
} else if (score <80&&score >70) {
System.out.println("C级");
} else if (score <70&&score >60) {
System.out.println("D级");
} else if (score <60&&score >=0) {
System.out.println("不及格");
} else {
System.out.println("你输入的不是本次的分数");
}
s.close();
}
}
package control;
public class SwitchDemo01 {
public static void main(String[] args) {
char grade = 'f';
switch (grade) {
case 'A' -> System.out.println("优秀");
case 'B' -> System.out.println("良好");
case 'C' -> System.out.println("及格");
case 'D' -> System.out.println("再接再厉");
case 'E' -> System.out.println("挂科");
default -> System.out.println("无");
}
}
}
package control;
public class SwitchDemo02 {
public static void main(String[] args){
String name = "部落冲突";
switch(name){
case "部落冲突":
System.out.println("部落冲突");
break;
case "超哥":
System.out.println("超哥");
break;
default:{
System.out.println("弄啥嘞!");
}
}
}
}
循环结构
package control;
public class WhileDome01 {
public static void main (String[] args){
//输出0~100;
int i = 0;
while(i<100){
i++;
System.out.println(i);
}
}
}
package control;
public class WhileDemo02 {
public static void main(String[] args){
//1+2+......+100
int i = 0;
int sum = 0;
while(i<100){
i++;
sum=sum+i;
}
System.out.println("1+2+3+......+100="+sum);
}
}
package control;
public class DoWhileDemo01 {
public static void main(String[] args){
int i = 0;
int sum = 0;
do{
i++;
sum=sum+i;
}while(i<100);
System.out.println("1+2+3+.......+100="+sum);
}
}
package control;
public class DoWhileDemo02{
public static void main(String[] argc){
int i = 0;
while(i<0){
System.out.println(i);
}
System.out.println("--------------");
do{
System.out.println(i);
i++;
}while(i<0);
}
}
package control;
public class ForDemo01 {
public static void main(String[] args) {
int i = 0;
while (i<100){
i+=2;
System.out.println(i);
}
System.out.println("----------------");
for (int i1 = 0; i1 < 100; i1++) {
System.out.println(i1);
}
}
}
package control;
public class ForDemo02 {
public static void main(String[] args) {
//计算0~100奇数与偶数和分别是多少?
int oddsum=0;
int evensum=0;
for (int i = 0; i <= 100; i++) {
if(i%2!=0){
oddsum+=i;
}else{
evensum+=i;
}
}
System.out.println("0~100的奇数和为:"+oddsum);
System.out.println("0~100的偶数和为:"+evensum);
}
}
package control;
public class ForDemo03 {
public static void main(String[] args) {
for (int i = 0; i <= 1000; i++) {
if(i%5==0){
System.out.print(i+"\t");
}
if(i%15==0){
System.out.println();
}
}
}
}
package control;
public class ForDemo04 {
public static void main(String[] args) {
/*
* 1*1=1
* 1*2=2 2*2=4
* 1*3=3 2*3=6 3*3=9
* 1*4=4 2*4=8 3*4=12 4*4=16
* 1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
* 1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
* 1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
* 1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
* 1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
*/
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <=j; i++) {
System.out.print(i + "*" + j + "=" + (i * j) + "\t");
}
System.out.println();
}
}
}
package control;
public class ForDemo05 {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5, 6};
for (int i : numbers) {
System.out.println(i );
}
System.out.println("==================");
for (int i1 = 0; i1 < 6; i1++) {
System.out.println(numbers[i1]);
}
}
}
break&continue
package control;
public class BreakDemo01 {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
System.out.println(i);
if(i==35){
break;
}
}
System.out.println("888");
}
}
package control;
public class ContinueDemo01 {
public static void main(String[] args) {
int i = 0;
while(i<100){
i++;
if(i%5==0){
System.out.println();
continue;
}
System.out.print(i+"\t ");
}
System.out.println("999");
}
}
练习
package struct.Test;
public class TestDemo01 {
public static void main(String[] args) {
//打印三角形
for (int i = 1; i <= 10; i++) {
for (int j = 10; j >=i; j--) {
System.out.print(" ");
}
for(int j =2;j<=i;j++){
System.out.print("*");
}
for(int j =2;j<i;j++){
System.out.print("*");
}
System.out.println();
}
}
}
标签:Java,String,int,基础,System,语法,println,public,out
From: https://www.cnblogs.com/1802ming/p/17280456.html