java第十四章示例代码 java第八章
用java如何写代码去判断质数和合数
首先介绍下:质数是除了本身和1以外,没有质因数,就是没有数能够整除之;合数是除了本身和1以外还有第三个数能整除之。
创新互联公司专注于企业全网整合营销推广、网站重做改版、晋州网站定制设计、自适应品牌网站建设、H5页面制作、商城系统网站开发、集团公司官网建设、外贸营销网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为晋州等各大城市提供网站开发制作服务。
具体示例代码如下:
public class Demo2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("请输入一个数:");
int num = scan.nextInt();
int index = 0;//使用一个数来标记是质数还是合数
for (int i = 2; i num; i++) {
if (num % i == 0) {
index++;
}
}
if (index == 0) {//index等于0表示质数
System.out.println("这是个质数");
}
else {//index大于0表示合数
System.out.println("这是个合数");
}
}
}
需要注意的是:1不算质数也不算合数。
Thinking in java 4th的第14章(Type Information)的练习7在eclipse中怎么做才是正确的
给你说个简单的方法吧,你右键你的工程,选择 import,选择exist file system,选择你的源代码所在的目录,这个目录需要时你的源代码package的父目录,导进来后,右键你的找个父目录,在build path里面选择use as source,就可以用了,大概步骤就是这样,具体你操作一下吧,很简单的
求Accp6.0java第十四章练习题一二的代码!
我自己写的,运行没问题,如果有小问题,欢迎指出
练习一:
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类的属性改成数组):
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);
}
}
标题名称:java第十四章示例代码 java第八章
URL网址:http://scyanting.com/article/dosejsd.html