The first article, java learning journey

In this hall of java, I just opened the gate, as if I found a road at a fork in the road to the gate of java.

Here are some problems with java algorithm

first question:

package project.model;

import java.util.Scanner;
// Rabbit problem: there is a pair of rabbits, a pair of rabbits are born every month from the 3rd month of birth, and a pair of rabbits are born every month after the third month. If none of the rabbits are, what is the total number of rabbits per month?
public class lianxi01 {
public static void main (String [] args) {
System.out.println ("Number of rabbits in the first month: 1");
System.out.println ("Number of rabbits in the second month : 1 ");
int f1 = 1, f2 = 1, f, M = 24;
for (int i = 3; i <= M; i ++) {
f = f2;
f2 = f1 + f2;
f1 = f;
System .out.println ("Day" + i + "monthly logarithm of rabbit" + f2);
}

}
}

-----------------------------------------------------------------------------------------

The second question:

package project.model;
// Determine how many prime numbers there are between 101-200, and output all prime numbers
public class lianxi02 {
public static void main (String [] args) {
int count = 0;
for (int i = 101; i <200; i + = 2) {
boolean b = false;
for (int j = 2; j <= Math.sqrt (i); j ++) {
if (i% j == 0) {
b = false; break;
}
else {
b = true;
}
}
if (b == true) {
count ++;
System.out.println (i);
}
System.out.println ("The number of prime numbers is:" + count);
}
}
}

-----------------------------------------------------------------------------------------

The third question:

package project.model;
// Print out all "narcissus number", the so-called "narcissus number" refers to a three-digit number, and the cubic sum of each digit is equal to the number itself. For example: 153 is a "narcissus number", because 153 = 1 cubic +5 cubic +3 cubic
public class lianxi03 {
public static void main (String [] args) {
int b1, b2, b3;
for (int m = 101; m <1000; m ++) {
b3 = m / 100;
b2 = m%
100/10 ; b1 = m% 10;
if ((b3 * b3 * b3 + b2 * b2 * b2 + b1 * b1 * b1) == m) {
System.out.println (m + "is a daffodil number");

}

}
}

}

-----------------------------------------------------------------------------------------

The fourth question:

package project.model;

import java.util.Scanner;

// Factorize a positive integer into prime factors. For example: enter 90, print 90 = 233 * 5
public class lianxi04 {
public static void main (String [] args) {
Scanner s = new Scanner (System.in);
System.out.println ("Please enter a positive integer" );
int n = s.nextInt ();
int k = 2;
System.out.println (n + "=");
while (k <= n) {
if (k == n) {
System.out.println ( n);
break;
}
else if (n% k == 0) {
System.out.print (k + "*");
n = n / k;
}
else {
k ++;
}

}
}

}

-----------------------------------------------------------------------------------------

Question 5:

package project.model;

import java.util.Scanner;

// Complete this question using the nesting of conditional operators: students with academic scores == 90 points are represented by A, B between 60-89 is represented by B, and C below 60 is represented by
public class lianxi05 {
public static void main (String [] args) {
int x;
String grade;
Scanner s = new Scanner (System.in);
System.out.println ("Please enter a grade");
x = s.nextInt ();
grade = x> = 90? "A"
: x> = 60? "B"
: "C";
System.out.println ("level is" + grade);
}
}

Published 31 original articles · Like 13 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/chushudu/article/details/103619816
Recommended