3 篇文章 0 订阅

# JAVA的世界很大—在新手村的我疯狂刷经验

## 代码的世界只有三种逻辑 无他，顺序，条件，循环

#### 任何复杂的程序逻辑 三种程序结构实现

if结构；

if (逻辑表达式){

语句1；

语句2；

}

语句3


if …else…;

if (boolean){
​ 语句块
}

package day03;
import java.util.Scanner;

public class Cashier {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
/*        int num = 6;
if (num % 2 == 0){
System.out.println("七");

}else {
System.out.println("6");

}*/

System.out.println("请输入单价：");
double untiPrice = scan.nextDouble();
System.out.println("请输入数量：");
double amout = scan.nextDouble();
System.out.println("请输入金额：");
double money = scan.nextDouble();

//总价
double totalPrice = untiPrice * amout;
if (totalPrice >= 500){
totalPrice *= 0.8;
System.out.println("打完8折："+totalPrice);
}
//找零
else {
System.out.println("错误");
}
if (money>=totalPrice){
double change = money - totalPrice;
System.out.println("找您"+change);

}else {
System.out.println("错误 还差"+((totalPrice)-(money)));

}

double change = money - totalPrice;
//System.out.println("现在价为："+totalPrice+"找零"+change);

System.out.println("面向工资学习，加油");

}
}



package day04;

public class Homework {
public static void main(String[] args) {
//编写三个数值排序
int a =8,b =3,c =5;
int t;
if (a>b){
t =a;
a =b;
b =t;
}
if (a>c){
t = a;
a =c;
c =t;
}
if (b>c){
t =b;
b =c;
c =t;
}
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("c="+c);
System.out.println("面向工资开发---加油");
}
}



package day04;
import java.util.Scanner;

public class Homework {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入成绩以判断等级：");
int score = scan.nextInt();
if (score>=0 && score <=100){
if (score >=90){
System.out.println("优秀");

}else if (score>=80 ){
System.out.println("良好");

}else if (score >=60 && score <70){
System.out.println("及格");

}else {
System.out.println("不及格");
}
}else {
System.out.println("请输入合法数字");
}

package day04;
import java.util.Scanner;

public class Homework {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入成绩以判断等级：");
int score = scan.nextInt();
if (score<0 || score >=100){
System.out.println("请输入合法数字");

}else if (score >=90){
System.out.println("优秀");

}else if (score>=80 ){
System.out.println("良好");

}else if (score >=60 && score <70){
System.out.println("及格");

}else {
System.out.println("不及格");
}


switch 多分支结构----与break搭配

    int num =2;
switch (num){
case 1:
System.out.println("111");
break;
case 2:
System.out.println("222");
break;
case 3:
System.out.println("333");
break;
case 4:
System.out.println("444");
break;
default:
System.out.println("666");
}


#### 循环结构

if(boolean){//执行一次
语句块
}
while(boolean){//执行多次
语句块
}


1>循环变量的初始化

2>循环变量的条件

3>循环变量的改变

package day04;
import java.util.Scanner;
public class Guessing {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("猜数字");
int guess =scan.nextInt();
int value = (int) (Math.random()*100+1);
while (guess != value){
if (guess ==value){
break;
}
if (guess > value){
System.out.println("大了");
}else {
System.out.println("小了");
}
System.out.println("请猜：");
guess = scan.nextInt();

}
if (guess == value) {
System.out.println("恭喜你，猜对了，离面向工资开发又近了一步");
}else {
System.out.println("很遗憾，下次努力");
}
}
}



do - while

do{//至少执行一次
语句块
}whie（）

int pwd;
do{
System.out.print("密码")
pwd = scanner.nextInt();
}while()


for

package day05;

public class Homework {
public static void main(String[] args) {
System.out.println("面向工资学习---加油");
for (int i=1;i<=10;i++){
System.out.println("面向工资学习---加油");
}
}
}



#### 循环问题

while 当…

do while 直到

for 固定循环次数

package day05;
import java.util.Scanner;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int score = 0;
for (int i=1;i<=10;i++){//10次
int a =(int)(Math.random()*100);
int b =(int)(Math.random()*100);
int result = a+b;
System.out.println("("+i+")"+a+"+"+b+"=");
System.out.println("算吧");
break;
}

score +=10;
System.out.println("答对了，加油");
}else{
System.out.println("错了...");
}
}
System.out.println("得分为："+score);
}
}



#### 嵌套

package day05;

public class MultiTable {
public static void main(String[] args) {
//99乘法表；

for (int i=1;i<=9;i++){//行

for (int j = 1;j<=i;j++){//列

System.out.print(i+"*"+j +"="+i * j+"\t");
}
System.out.println();
}
}
}



#### 程序 = 算法 + 数据结构

1>算法 解决问题的流程/步骤

2>数据结构：将数据安照某种特定的结构保存

​ 设计良好的数据会导致好的算法

​ 是一种数据类型

​ 是相同数据类型元素的集合

​ int a;----声明一个整型变量

​ int[]b;-----声明一个整型数组变量b

int[] a = new int [5];

double[] b = new double[8];

​ int[] arr = new int[4];//0,0,0,0

​ int[] arr ={3,5,7,9};

​ int[] arr = new int[]{3,5,7,9};

int[]arr = {23,45,78,5};

1)假设第一个元素为最大值

​ int max = arr[0];

2)遍历剩余元素，将剩余元素与max1作比较，

​ for(int i =1;i<arr.length;i++){

​ if(arr[i]>max){

​ max = arr[i]

​ }

​ }

(核心思想 同化 大吃小)

max =23 - max =45 - max =78 -

package day05;

public class Homework {//数组求最大值
public static void main(String[] args) {

int[]arr = new int[]{1,25,6,78,100,0,1,8};
int max = arr[0];
for (int i =1;i<arr.length;i++){
if (arr[i]>max){
max = arr[i];
}
}
System.out.println("最大值："+max);
}
}


long sum =0;//求9+99+999+...+99999999
long num =0;
for (int i =0;i<=10;i++){//10次
/*
*       num =o
* i =1  num =9	num*10+9
* i =2  num =99
* i =3  num =999
* */
num = num*10+9;
sum +=num;
}
System.out.println("sum="+sum);


System.arryaycopy(a,1,a1,0,4);

int[] a1 = Arrays.copyOf(a,6);

import java.util.Arrays;

public class MaxOfArray {
public static void main(String[] args) {
int []arr = new int[10];
int max =0;
for (int i =0;i<arr.length;i++){
arr[i]=(int)(Math.random()*100);
System.out.println(arr[i]);
}
for (int i =0;i<arr.length;i++){
if (max < arr[i]){
max = arr[i];
}
}
//数组扩容
System.out.println("max="+max);

arr = Arrays.copyOf(arr,arr.length+1);
arr[arr.length-1] = max;
for (int i = 0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
}


//JAVA: Arrays.sort(arr);

​ 特点：5个数冒四轮

​ 每一轮和第一个元素开始

​ 每一次和下一个元素比较

​ 冒出了就不参与冒泡

int[]arr = {65,26,56,7,45};
for(int i=0;i<arr.length-1;i++){//控制轮数
for(int j=0;j<arr.length-1-;j++){//控制次数
if(arr[j]>arr[j+1]){
int t =arr[j];
arr[j] =arr[j+1];
arr[j+1] =t;
}

}
}

public class Bubbdemo {//冒泡排序！！！
public static void main(String[] args) {
int[] arr = new int[10];
for (int i = 0;i<arr.length;i++){
arr[i] = (int) (Math.random()*100);
System.out.print(arr[i]+" ");
}
System.out.println("");
for (int i =0;i<arr.length-1;i++){
for (int j =0;j<arr.length-1-i;j++){
if (arr[j]<arr[j+1]){
int t =arr[j];
arr[j] =arr[j+1];
arr[j+1] = t;
}
}

}
System.out.println("排好的数值");
for (int i=0;i <arr.length;i++){
System.out.print(arr[i]+" ");
}
}
}


#### 方法的定义：

​ 无返回值void

​ 方法完成后：

​ 如果要用到某个数据，有返回值

public static int sum(int num1,int num2){

}


public class MethodDemo {
public static void main(String[] args) {
say();
sayHi("小可");
double a = getNum();
int b = plus(1,4);
System.out.println(a);
System.out.println(b);

}
public static int plus(int num1,int num2){//有参有返回值
int num = num1+num2;
return num;
}
public static double getNum(){
//return ;//编译错误 return 必须跟值
return 66.6; //结束方法的执行  返回结果
}
public static void sayHi(String name){//有参数无返回值
System.out.println("大家好，我叫"+name+"!");
}
public static void say(){//无参无返回值
System.out.println("hello 我叫小柯");
}
}


#### 方法的调用：

return ;

return 值 // 结束方法的执行 ~~~~返回结果给调用方法

JAVA很苦，可却是苦尽甘来

*~~

~~ *

• 1
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

02-01
02-12 275

05-28
02-29
06-08
10-08
04-14 63
08-23 720
07-21 48
12-10 1943
03-28 318