1、九九乘法表main要放在class里面
package 九九乘法表;
//
public class text {
public static void main(String[] args) {
for (int i = 0; i <=9 ; i++) {
for (int j = 1; j <=i ; j++) {
// System.out.printf("%d * %d = %d ",i,j,(i*j));
System.out.printf(i + "X" + j + "=" + (i*j) + "\t");
}
System.out.println();
}
}
}
/*
输出结果:
1 * 1 = 1
2 * 1 = 2 2 * 2 = 4
3 * 1 = 3 3 * 2 = 6 3 * 3 = 9
4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36
7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49
8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64
9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
1X1=1
2X1=2 2X2=4
3X1=3 3X2=6 3X3=9
4X1=4 4X2=8 4X3=12 4X4=16
5X1=5 5X2=10 5X3=15 5X4=20 5X5=25
6X1=6 6X2=12 6X3=18 6X4=24 6X5=30 6X6=36
7X1=7 7X2=14 7X3=21 7X4=28 7X5=35 7X6=42 7X7=49
8X1=8 8X2=16 8X3=24 8X4=32 8X5=40 8X6=48 8X7=56 8X8=64
9X1=9 9X2=18 9X3=27 9X4=36 9X5=45 9X6=54 9X7=63 9X8=72 9X9=81
*/
2、打印星星
// Scanner sc=new Scanner(System.in); 先实例化一个对象
//double scouse=sc.nextDouble(); 接受数据
package 打印星;
import java.util.Scanner;
//找规律
// 打印空格的是从i(n-1)见到0
//打印* 是从(行数-1)*2+1 =2*行数+1
public class 打印星星 {
public static void main(String[] args) {
System.out.println("打印几行:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i <n ; i++) {
for(int k=n-i;k>0;k--){
System.out.print(" ");
}
for (int j = 0; j <(2*i+1) ; j++) {
// System.out.print("*");
System.out.print(j%2==0? " ":"*");
//如果j是偶数的话,把* 替换成空格
}
System.out.println();
}
}
}
//结果
打印几行:5
*
***
*****
*******
*********
打印几行:8
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
进程已结束,退出代码0
3、双色球_select_
-
用while需要加break跳出循环
-
方法名是前面是访问权限符
package 双色球;
import java.util.Scanner;
import static java.lang.System.*;
public class 双色 {
public static void main(String[] args) {
int []numbers= selectNumbers();
printNumbers(numbers);
}
private static void printNumbers(int[] numbers) {
for (int i = 0; i < numbers.length; i++) {
System.out.print(numbers[i]+",");
}
}
public static int [] selectNumbers(){
int [] numbers =new int[7];
Scanner sc = new Scanner(in);
for (int i = 0; i <numbers.length-1 ; i++) {
while (true) {
out.println("输入0-33的不重复的红球数字:");
int number = sc.nextInt();
// for (int i = 0; i < numbers.length - 1; i++) {
if (number < 1 || number > 33) {
out.println("输入红球的数字无效,重新输入:");
} else {
if (exsit(numbers, number)) {
//true
out.println("输入的红球数字重复,请重新输入:");
} else {
numbers[i] = number; //这里的赋值操作,右边的赋给左边的
break;//跳出当前循环
}
}
// }
}
}
while(true){
out.println("输入最后一个蓝色球的号码(1-16):");
int number= sc.nextInt();
if(number>16||number<1){
out.println("输入蓝色的数字无效,重新输入:");
}
else{
numbers[6]=number;
break;
}
}
return numbers;
}
//或者这里用 遍历numbers[]数组里面是否有为0的项
private static boolean exsit(int[] numbers, int number) {
for (int i = 0; i < numbers.length-1 ; i++) {
if (number == numbers[i]) {
return true;
}
}
return false;
}
}
结果:
输入0-33的不重复的红球数字:
1
输入0-33的不重复的红球数字:
2
输入0-33的不重复的红球数字:
3
输入0-33的不重复的红球数字:
3
输入的红球数字重复,请重新输入:
输入0-33的不重复的红球数字:
3
输入的红球数字重复,请重新输入:
输入0-33的不重复的红球数字:
4
输入0-33的不重复的红球数字:
5
输入0-33的不重复的红球数字:
6
输入最后一个蓝色球的号码(1-16):
8
1,2,3,4,5,6,8,
进程已结束,退出代码0