day4
java新手随笔练习
public class Doc {
String name; //属性
/**
* since 指明需要最早使用的jak版本
* @author wushen 作者名
* @param name 参数名
* @return 返回值情况
* @throws Exception 异常抛出情况
*/
public String test(String name)throws Exception{ //方法
return name;
//javadoc -encoding UTF-8 -charset UTF-8 java文件 //使用IDEA生产Java文档
}
}
Scanner练习
public static void main(String[] args) {
//创建一个扫描器对象,用于接受键盘数据
Scanner scanner= new Scanner(System.in);
System.out.println("使用next方式接收:");
//判断用户有没有输入字符串
if (scanner.hasNext()){
//使用next方式接收
String str =scanner.next();
System.out.println("输入的内容为:"+str);
}
//凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
scanner.close();
}
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("使用nextline方式接收:");
if (scanner.hasNext()){
String str=scanner.nextLine();
System.out.println("接收到的内容:"+str);
}
scanner.close();
}
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入数据:");
String str= scanner.nextLine();
System.out.println("输入的值为:"+str);
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
//从键盘接收数据
int i=0;
double d=0.0;
System.out.println("请输入整数:");
if (scanner.hasNextInt()){
i=scanner.nextInt();
System.out.println("整数数据:"+i);
}else {
System.out.println("输入的不是整数数据");
}
System.out.println("请输入小数:");
if (scanner.hasNextDouble()){
d=scanner.nextDouble();
System.out.println("小数数据:"+d);
}else {
System.out.println("输入的不是小数数据");
}
scanner.close();
}
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
//我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果;
Scanner scanner=new Scanner(System.in);
//和
double sum=0;
//计算输入了多少数字
int m=0;
//通过循环判断是否还有输入,并在里面对每一次求和和统计
while (scanner.hasNextDouble()){
double x= scanner.nextDouble();
m=m+1;//m++
sum=sum+x;
System.out.println(m+"个数的和为"+sum);
System.out.println(m+"个数的平均值为"+sum/m);
}
scanner.close();
}
import java.util.Scanner;
public class test01 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
double sum=0;
int m=0;
while (scanner.hasNextDouble()){
double x=scanner.nextDouble();
m=m+1;
sum=sum+x;
System.out.println(m+"个数总和为"+sum);
System.out.println(m+"个数平均数为"+sum/m);
}
scanner.close();
}
public class Demo01 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入内容");
String s=scanner.nextLine();
//equals:判断字符串是否相等
if (s.equals("Hello")){
System.out.println(s);
}
System.out.println("End");
scanner.close();
}
初学者的自我练习
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入一个成绩");
int score= scanner.nextInt();
if (score<0){
System.out.println("请输入正确的值");}
else {
if (score<60){
System.out.println("不及格");
}else {
if (score==60&&score<70){
System.out.println("及格");
}else {
if (70<score&&score<80){
System.out.println("良好");
}else {
if (80<score&&score<90){
System.out.println("优秀");
}else {
if (score==100&&score<100){
System.out.println("人才");
}else {
System.out.println("请在0-100中输入");
}
}
}
}
}
}
scanner.close();
}
猜数字编码
import java.util.Random;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
Random r = new Random();
int a = r.nextInt(101);//生成随机数范围0-100
do{
//输入猜测的数字
Scanner sc = new Scanner(System.in);
System.out.println("输入猜测的数字");
int x = sc.nextInt();
if (x == a){
System.out.println("猜测正确");
break;//猜测正确直接退出
}else if (x<a){
System.out.println("猜小了");
}else {
System.out.println("猜大了");
}
}while (true);
}
初学do……while
即使不满足条件,依然可以进行一次循环
public class DOwhile01 {
public static void main(String[] args) {
int i=0;
int sum=0;
do {
sum=sum+i;
i++;
}while (i<=100);
System.out.println(sum);
}
public class DOwhile02 {
public static void main(String[] args) {
int a=0;
while (a<0){
a++;
System.out.println(a);
}
do {
a++;
System.out.println(a);
}while (a<0);
}
接下来是0-100相加的for循环
public class ForDemo01 {
public static void main(String[] args) {
int a;
int sum=0;
for (a=1;a<=100;a++){
sum=sum+a;
System.out.println(sum);
}
/*
for(;;){} //死循环
*/
}
练习1:计算0到100之间的奇数和偶数的和
方法一:
public class ForDemo02 {
public static void main(String[] args) {
//奇数
int a,sum1=0,sum2=0;
for (a=1;a<=100;a+=2){
sum1=sum1+a;
System.out.println("奇数的和"+sum1);
}
//偶数
System.out.println("===========================");
for (a=0;a<=100;a+=2){
sum2=sum2+a;
System.out.println("偶数的和"+sum2);
}
}
方法二:
public class ForDemo03 {
public static void main(String[] args) {
int oddsum=0;
int evensum=0;
for (int i=0;i<=100;i++){
if (i%2==0){
evensum+=i;
}else {
oddsum+=i;
}
System.out.println("偶数的和"+evensum);
System.out.println("奇数的和"+oddsum);
}
}
//练习2:用while和for循环输出1-1000之间能被5整除的数,并且每行输出3个
public class ForDemo04 {
public static void main(String[] args) {
for (int i = 0; i <= 1000; i++) {
if (i%5==0){
System.out.print(i+"\t");
}
if (i%(3*5)==0){
System.out.print("\n");
}
}
}
打印九九乘法表
public class ForDemo05 {
public static void main(String[] args) {
int i,j,sum;
for (i=1;i<=9;i++){
// System.out.println(1+"*"+i+"="+(1*i));
for (j=1;j<=i;j++){
System.out.print(j+"*"+i+"="+(j*i)+"\t");
}
System.out.println();
}
}
初识数组
public class ForDemo06 {
public static void main(String[] args) {
int[] numbers={10,20,30,40,50};//定义了一个数组
for (int i=0;i<5;i++){
System.out.println(numbers[i]);
}
//遍历数组的元素
for (int x:numbers){
System.out.println(x);}
}
初识case
public class Switchdemo01 {
public static void main(String[] args) {
//case穿透
char grade='C';
switch (grade){
case 'A':
System.out.println("优秀");break;
case 'B':
System.out.println("良好");break;
case 'C':
System.out.println("及格");break;
case 'D':
System.out.println("挂科");break;
case 'E':
System.out.println("重修");break;
default:
System.out.println("未知");
}
}
练习case
public class SwitchDemo02 {
public static void main(String[] args) {
String name="迪迦";
switch (name){
case "铠甲勇士":
System.out.println("铠甲勇士");break;
case "吴佳":
System.out.println("吴佳");break;
case "迪迦":
System.out.println("迪迦");break;
}
}
初识while
十分有意思,不满足条件,不进行循环
public class WhileDemo01 {
public static void main(String[] args) {
int x=0;
while (x<100){
x++; //x=x+1
System.out.println(x);
}
}
练习while
public class WhileDemo02 {
public static void main(String[] args) {
//死循环while(true){}
//计算1+...+100=?
int i=0;
int sum=0;
while(i<=100){
sum=sum+i;
i++;
System.out.println(sum);
}
}
标签:scanner,JAVA,String,System,初识,println,public,循环,out
From: https://www.cnblogs.com/determine-boke/p/17990767