2024/11/4
目录
一、找素数
①方法一
public class Test07 {
public static void main(String[] args) {
System.out.println(Find(101, 200));
}
public static int Find(int start,int end){
int count = 0;
for(int i = start;i <= end;i++){
boolean flag = true;
for(int j = 2;j <= i / 2;j++){
if(i % j == 0){
flag = false;
break;
}
}
if(flag){
System.out.println(i);
count++;
}
}
return count;
}
}
②方法二
public class Test07_2 {
public static void main(String[] args) {
int count = 0;
OUT://为外部循环指定标签
for(int i = 101;i <= 200;i++){
for(int j = 2;j <= i / 2;j++){
if(i % j == 0){
continue OUT;
}
}
count++;
System.out.println(i);
}
System.out.println(count);
}
}
二、打印九九乘法表
public class Test08 {
public static void main(String[] args) {
for(int i = 1;i <= 9;i++){
for(int j = 1;j <= i;j++){
System.out.print(j + "X" + i + "=" + (j * i) + "\t");
}
System.out.println();
}
}
}
三、打印三角形
public class Test09 {
public static void main(String[] args) {
// *
// ***
// *****
// *******
// 行(i) 空格(n-i) 星星(2i -1)
// 1 3 1
// 2 2 3
// 3 1 5
// 4 0 7
int n = 4;
for(int i = 1;i <= 4;i++){
for(int j = 1;j <= (n-i);j++){
System.out.print(" ");
}
for(int k = 1;k <=(2*i - 1);k++){
System.out.print("*");
}
System.out.println();
}
}
}
四、双色球系统
import java.util.Random;
import java.util.Scanner;
public class Test10 {
public static void main(String[] args) {
int[] userNumbers = userSelectNumbers();
int[] luckyNumbers = createLuckyNumbers();
Judge2(userNumbers,luckyNumbers);
}
public static int[] userSelectNumbers(){
int[] nums = new int[7];
Scanner sc = new Scanner(System.in);
for(int i = 0;i < nums.length - 1;i++){
while (true) {
System.out.println("请输入第" + (i + 1) + "个红球号码");
int num = sc.nextInt();
if(num < 1 || num > 33){
System.out.println("您输入的号码有误!请重新输入");
}else{
if(Judge1(nums,num)){
System.out.println("您输入的号码重复,请重新输入");
}else{
nums[i] = num;
break;
}
}
}
}
while (true) {
System.out.println("请输入最后一个蓝球号码");
int num = sc.nextInt();
if(num < 1 || num > 16){
System.out.println("您输入有误,请重新输入");
}else{
nums[nums.length - 1] = num;
break;
}
}
return nums;
}
public static boolean Judge1(int[] nums, int num) {
for (int i = 0; i < nums.length; i++) {
if(nums[i] == num){
return true;
}
}
return false;
}
public static int[] createLuckyNumbers(){
int[] nums = new int[7];
Random r = new Random();
for(int i = 0;i < nums.length - 1;i++){
while (true) {
int num = r.nextInt(33) + 1;
if(!Judge1(nums,num)){
nums[i] = num;
break;
}
}
}
int num = r.nextInt(17) + 1;
nums[nums.length - 1] = num;
return nums;
}
public static void Judge2(int[] userNumbers,int[] luckyNumbers){
int redCount = 0;
int blueCount = 0;
for(int i = 0;i < userNumbers.length - 1;i++){
for(int j = 0;j < luckyNumbers.length - 1;j++){
if(userNumbers[i] == luckyNumbers[j]){
redCount++;
break;
}
}
}
blueCount = userNumbers[6] == luckyNumbers[6] ? 1 : 0;
if(redCount == 6 && blueCount ==1){
System.out.println("中1000万");
}else if(redCount == 6 && blueCount ==0){
System.out.println("中500万");
}else if(redCount == 5 && blueCount ==1){
System.out.println("中3000");
}else if(redCount == 5 && blueCount ==0){
System.out.println("中200");
}else if(redCount == 4 && blueCount ==1){
System.out.println("中200");
}else if(redCount == 4 && blueCount ==0){
System.out.println("中10");
}else if(redCount == 3 && blueCount ==1){
System.out.println("中10");
}else if(redCount == 2 && blueCount ==1){
System.out.println("中5");
}else if(redCount == 1 && blueCount ==1){
System.out.println("中5");
}else if(redCount == 0 && blueCount ==1){
System.out.println("中5");
}else{
System.out.println("没中");
}
}
}
标签:...,Java,nums,int,System,案例,num,println,out
From: https://blog.csdn.net/2301_77423979/article/details/143495025