首先介紹下:質數(shù)是除了本身和1以外,沒有質因數(shù),就是沒有數(shù)能夠整除之;合數(shù)是除了本身和1以外還有第三個數(shù)能整除之。
創(chuàng)新互聯(lián)公司專注于企業(yè)全網(wǎng)整合營銷推廣、網(wǎng)站重做改版、晉州網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、H5頁面制作、商城系統(tǒng)網(wǎng)站開發(fā)、集團公司官網(wǎng)建設、外貿營銷網(wǎng)站建設、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為晉州等各大城市提供網(wǎng)站開發(fā)制作服務。
具體示例代碼如下:
public?class?Demo2?{
public?static?void?main(String[]?args)?{
Scanner?scan?=?new?Scanner(System.in);
System.out.print("請輸入一個數(shù):");
int?num?=?scan.nextInt();
int?index?=?0;//使用一個數(shù)來標記是質數(shù)還是合數(shù)
for?(int?i?=?2;?i??num;?i++)?{
if?(num?%?i?==?0)?{
index++;
}
}
if?(index?==?0)?{//index等于0表示質數(shù)
System.out.println("這是個質數(shù)");
}
else?{//index大于0表示合數(shù)
System.out.println("這是個合數(shù)");
}
}
}
需要注意的是:1不算質數(shù)也不算合數(shù)。
給你說個簡單的方法吧,你右鍵你的工程,選擇 import,選擇exist file system,選擇你的源代碼所在的目錄,這個目錄需要時你的源代碼package的父目錄,導進來后,右鍵你的找個父目錄,在build path里面選擇use as source,就可以用了,大概步驟就是這樣,具體你操作一下吧,很簡單的
我自己寫的,運行沒問題,如果有小問題,歡迎指出
練習一:
Customer類:
package com.wxws.sms;
public class Customer {
/**
* @param args
*/
String CustNumber;
int CustPoint;
}
--------------------------------分隔符------------------------------------
CustManager類:
package com.wxws.sms;
import java.util.*;
public class CustManager {
Customer customers[]=new Customer[100];
public void CustAdd(Customer cust) {
// TODO Auto-generated constructor stub
for(int i=0;icustomers.length;i++){
if(customers[i]==null){
customers[i]=cust;
break;
}
}
}
public void showCustomer(){
System.out.println("***Customers List***");
System.out.println("number\tpoint");
for(int i=0;icustomers.length;i++){
if(customers[i]!=null){
System.out.println(customers[i].CustNumber+"\t"+customers[i].CustPoint);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Customer run=new Customer();
CustManager move=new CustManager();
@SuppressWarnings("resource")
Scanner input=new Scanner(System.in);
System.out.print("Please input your Vip number:");
run.CustNumber=input.next();
System.out.print("Please input your Vip point:");
run.CustPoint=input.nextInt();
move.CustAdd(run);
move.showCustomer();
}
}
---------------------------------------------分割符號---------------------------------------------------
練習二(將Customer類的屬性改成數(shù)組):
package com.wxws.sms;
public class Customer {
/**
* @param args
*/
String CustNumber[]=new String[100];
int CustPoint[]=new int[100];
}
---------------------------------------------分割符號---------------------------------------------------
package com.wxws.sms;
import java.util.*;
public class CustManager {
Customer customers[]=new Customer[100];
public void CustAdd(Customer cust) {
// TODO Auto-generated constructor stub
for(int i=0;icustomers.length;i++){
if(customers[i]==null){
customers[i]=cust;
break;
}
}
}
public void showCustomer(){
System.out.println("***Customers List***");
System.out.println("number\tpoint");
for(int i=0;icustomers.length;i++){
if(customers[i]!=null){
System.out.println(customers[i].CustNumber[i]+"\t"+customers[i].CustPoint[i]);
}
}
}
public int searchScore(String custNo){
int point=1;
for(int i=0;icustomers.length;i++){
if(customers[i]!=null){
if((customers[i].CustNumber[i]).equals(custNo)){
System.out.println("The number of Vip's point is:"+customers[i].CustPoint[i]);
point=customers[i].CustPoint[i];
}
}
}
return point;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Customer run=new Customer();
CustManager move=new CustManager();
@SuppressWarnings("resource")
Scanner input=new Scanner(System.in);
for(int i=0;i4;i++){
System.out.print("Please input your Vip number:");
run.CustNumber[i]=input.next();
System.out.print("Please input your Vip point:");
run.CustPoint[i]=input.nextInt();
move.CustAdd(run);
}
move.showCustomer();
System.out.print("Please input the Vip number you want to check:");
String custNum=input.next();
move.searchScore(custNum);
}
}