
java判断质数
这段程序为什么无法调用input1和input2,isPrime以及isPrime2这两个方法importjava.util.Scanner;publicclassPri...
这段程序为什么无法调用input1和input2,isPrime以及isPrime2这两个方法
import java.util.Scanner;
public class Prime_Decide {
public boolean isPrime(int a){ //判断int整形a是否是素数
int temp,i;
while(a < 1){ System.out.println("你的输入有误,请重新输入:");}
temp=(new Double(Math.sqrt(a))).intValue();
for(i=temp;i>1;i--){
if(a%i==0) break;
}
if(i==0)
System.out.println("你输入的数不是质数");
else
System.out.println("你输入的数是质数");
if(i==1) return true;
else return false;
}
public void isPrime_2(int a, int b){
while(a < 0 && a ==b && a < b){
System.out.println("你的输入有误,重新输入:");
}
for(int i = a; i<= b; i++){
boolean isPrime2 = isPrime(i);
if(isPrime2)
System.out.print(" "+ i);
}
}
public void Input1(){
System.out.println("请输入你要进行判断的数:");
Scanner sc = new Scanner(System.in);
String st = sc.nextLine();
int temp = Integer.parseInt(st);
isPrime(temp);
}
public void Input2(){
int a,b;
System.out.println("请输入上界 a= ");
Scanner sc = new Scanner(System.in);
a = Integer.parseInt(sc.nextLine());
System.out.println("请输入下届 b=");
Scanner st = new Scanner(System.in);
b = Integer.parseInt(st.nextLine());
isPrime_2(a,b);
}
public void Display(){
System.out.println("请输入你要进行的数的判断:\n1: 单个数的判断\n2: 一段整数范围的判断\n3: 退出");
Scanner sc = new Scanner(System.in);
int c = Integer.parseInt(sc.nextLine());
if(c==1)
Input1();
if(c==2)
Input2();
if(c==3)
System.exit(1);
if(c!=1&&c!=2&&c!=3)
System.out.println("你的输入有误,请重新输入:");
}
public static void main(String[] args){
Prime_Decide pr = new Prime_Decide();
pr.Display();
}
} 展开
import java.util.Scanner;
public class Prime_Decide {
public boolean isPrime(int a){ //判断int整形a是否是素数
int temp,i;
while(a < 1){ System.out.println("你的输入有误,请重新输入:");}
temp=(new Double(Math.sqrt(a))).intValue();
for(i=temp;i>1;i--){
if(a%i==0) break;
}
if(i==0)
System.out.println("你输入的数不是质数");
else
System.out.println("你输入的数是质数");
if(i==1) return true;
else return false;
}
public void isPrime_2(int a, int b){
while(a < 0 && a ==b && a < b){
System.out.println("你的输入有误,重新输入:");
}
for(int i = a; i<= b; i++){
boolean isPrime2 = isPrime(i);
if(isPrime2)
System.out.print(" "+ i);
}
}
public void Input1(){
System.out.println("请输入你要进行判断的数:");
Scanner sc = new Scanner(System.in);
String st = sc.nextLine();
int temp = Integer.parseInt(st);
isPrime(temp);
}
public void Input2(){
int a,b;
System.out.println("请输入上界 a= ");
Scanner sc = new Scanner(System.in);
a = Integer.parseInt(sc.nextLine());
System.out.println("请输入下届 b=");
Scanner st = new Scanner(System.in);
b = Integer.parseInt(st.nextLine());
isPrime_2(a,b);
}
public void Display(){
System.out.println("请输入你要进行的数的判断:\n1: 单个数的判断\n2: 一段整数范围的判断\n3: 退出");
Scanner sc = new Scanner(System.in);
int c = Integer.parseInt(sc.nextLine());
if(c==1)
Input1();
if(c==2)
Input2();
if(c==3)
System.exit(1);
if(c!=1&&c!=2&&c!=3)
System.out.println("你的输入有误,请重新输入:");
}
public static void main(String[] args){
Prime_Decide pr = new Prime_Decide();
pr.Display();
}
} 展开
展开全部
不知道你要的是不是这种效果,程序在你的基础上做了一些修改,在你的基础上增加了假如输入的不是一个数时做什么处理,输入“back”时返回,你可以拿去把程序修改的更完善。。你说的方法都可以调用,在 main方法里面需要new一个对象来调用,除非你把方法定义为静态方法(static)。。
import java.util.Scanner;
public class Prime_Decide {
public void isPrime(String st) { // 判断int整形a是否是素数
try {
if (st.equals("back")) {
Display();
} else {
int a, i;
a = Integer.parseInt(st);
for (i = 2; i <= a / 2; i++) {
if (a % i == 0) {
break;
}
}
if (a == 0) {
System.out.println(a + " 你输入的数不是素数");
} else if (i > a / 2) {
System.out.println(a + " 你输入的数是素数");
} else {
System.out.println(a + " 你输入的数不是素数");
}
Input1();
}
} catch (NumberFormatException e1) {
System.out.println("请你输入正确的数字!!!");
Input1();
}
}
public void isPrime_2(String s1, String s2) {
try {
if (s1.equals("back")) {
Display();
} else {
int a = Integer.parseInt(s1);
int b = Integer.parseInt(s2);
if (a < 0 && a == b && a < b) {
System.out.println("你的输入有误,重新输入:");
}
int j;
for (int i = a; i <= b; i++) {
for (j = 2; j <= i / 2; j++) {
if (i % j == 0) {
break;
}
}
if (j > i / 2) {
System.out.println(" " + i + " 是素数");
}
}
Input2();
}
} catch (NumberFormatException e) {
System.out.println("请你输入正确的数字!!");
Input2();
}
}
public void Input1() {
System.out.println("请输入你要进行判断的数:");
Scanner sc = new Scanner(System.in);
String st = sc.nextLine();
isPrime(st);
}
public void Input2() {
// int a, b;
System.out.println("请输入上界 a= ");
Scanner sc = new Scanner(System.in);
// a = Integer.parseInt(sc.nextLine());
System.out.println("请输入下届 b=");
Scanner st = new Scanner(System.in);
// b = Integer.parseInt(st.nextLine());
isPrime_2(sc.nextLine(), st.nextLine());
}
public void Display() {
System.out.println("请输入你要进行的数的判断:\n1: 单个数的判断\n2: 一段整数范围的判断\n3: 退出");
try {
Scanner sc = new Scanner(System.in);
int c = Integer.parseInt(sc.nextLine());
if (c == 1) {
Input1();
} else if (c == 2) {
Input2();
} else if (c == 3) {
System.exit(1);
} else if (c != 1 & c != 2 & c != 3) {
System.out.println("你输入如的信息有误,请重新输入1、2、3!");
Display();
}
} catch (NumberFormatException e) {
System.out.println("你输入如的信息有误,请重新输入1、2、3!");
Display();
}
}
public static void main(String[] args) {
Prime_Decide pr = new Prime_Decide();
pr.Display();
}
}
希望对你有帮助!!
import java.util.Scanner;
public class Prime_Decide {
public void isPrime(String st) { // 判断int整形a是否是素数
try {
if (st.equals("back")) {
Display();
} else {
int a, i;
a = Integer.parseInt(st);
for (i = 2; i <= a / 2; i++) {
if (a % i == 0) {
break;
}
}
if (a == 0) {
System.out.println(a + " 你输入的数不是素数");
} else if (i > a / 2) {
System.out.println(a + " 你输入的数是素数");
} else {
System.out.println(a + " 你输入的数不是素数");
}
Input1();
}
} catch (NumberFormatException e1) {
System.out.println("请你输入正确的数字!!!");
Input1();
}
}
public void isPrime_2(String s1, String s2) {
try {
if (s1.equals("back")) {
Display();
} else {
int a = Integer.parseInt(s1);
int b = Integer.parseInt(s2);
if (a < 0 && a == b && a < b) {
System.out.println("你的输入有误,重新输入:");
}
int j;
for (int i = a; i <= b; i++) {
for (j = 2; j <= i / 2; j++) {
if (i % j == 0) {
break;
}
}
if (j > i / 2) {
System.out.println(" " + i + " 是素数");
}
}
Input2();
}
} catch (NumberFormatException e) {
System.out.println("请你输入正确的数字!!");
Input2();
}
}
public void Input1() {
System.out.println("请输入你要进行判断的数:");
Scanner sc = new Scanner(System.in);
String st = sc.nextLine();
isPrime(st);
}
public void Input2() {
// int a, b;
System.out.println("请输入上界 a= ");
Scanner sc = new Scanner(System.in);
// a = Integer.parseInt(sc.nextLine());
System.out.println("请输入下届 b=");
Scanner st = new Scanner(System.in);
// b = Integer.parseInt(st.nextLine());
isPrime_2(sc.nextLine(), st.nextLine());
}
public void Display() {
System.out.println("请输入你要进行的数的判断:\n1: 单个数的判断\n2: 一段整数范围的判断\n3: 退出");
try {
Scanner sc = new Scanner(System.in);
int c = Integer.parseInt(sc.nextLine());
if (c == 1) {
Input1();
} else if (c == 2) {
Input2();
} else if (c == 3) {
System.exit(1);
} else if (c != 1 & c != 2 & c != 3) {
System.out.println("你输入如的信息有误,请重新输入1、2、3!");
Display();
}
} catch (NumberFormatException e) {
System.out.println("你输入如的信息有误,请重新输入1、2、3!");
Display();
}
}
public static void main(String[] args) {
Prime_Decide pr = new Prime_Decide();
pr.Display();
}
}
希望对你有帮助!!
展开全部
质数(素数)就是一个大于1的自然数,除了1和它本身外,不能被其他自然数整除。
可以通过以下工具类方法来实现:
public boolean isPrime(int n){
for(int i=2;i<=n/2;i++){
if(n%i == 0)
return false;
}
return true;
}
备注:n/2的作用就是只取出n一半的数,因为任何数都不可能是比它1/2大的数整除的(除了这个数本身)。
可以通过以下工具类方法来实现:
public boolean isPrime(int n){
for(int i=2;i<=n/2;i++){
if(n%i == 0)
return false;
}
return true;
}
备注:n/2的作用就是只取出n一半的数,因为任何数都不可能是比它1/2大的数整除的(除了这个数本身)。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
完全没有问题,运行结果:
请输入你要进行的数的判断:
1: 单个数的判断
2: 一段整数范围的判断
3: 退出
1
请输入你要进行判断的数:
13
你输入的数是质数
请输入你要进行的数的判断:
1: 单个数的判断
2: 一段整数范围的判断
3: 退出
1
请输入你要进行判断的数:
13
你输入的数是质数
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这几个方法可以调用啊,如果你是在main方法中调用,要加上pr.*
PS:你判断质数的方法有问题。
PS:你判断质数的方法有问题。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public static void main(String[] args) {
int count=0;
int prime=1;
while(count<100){
while(true){
prime++;
if(isPrime(prime)){
System.out.print(prime+"||");
count++;
if(count%10==0){
System.out.println(" ");
break;
}
}
}
}
}
public static boolean isPrime(int n){
for(int i=2;i<n;i++){
if(n%i==0){
return false;
}
}
if(n==1){
return false;
}
return true;
}
int count=0;
int prime=1;
while(count<100){
while(true){
prime++;
if(isPrime(prime)){
System.out.print(prime+"||");
count++;
if(count%10==0){
System.out.println(" ");
break;
}
}
}
}
}
public static boolean isPrime(int n){
for(int i=2;i<n;i++){
if(n%i==0){
return false;
}
}
if(n==1){
return false;
}
return true;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询