Trending News
JAVA問題 20點歐!!!
最近在練習JAVA有遇到問題
想很久還是做不太出來~
希望JAVA高手可以幫我寫出程式碼~
可以的話有解說比較好 謝謝^^
1.試設計一程式,試求1000的 perfect number(完美數)。
2.在 Java 中,由鍵盤輸入的資料,會先以字串的形式儲存,若是想由鍵盤輸入字元,則可以利用charAt() method,取得index位置的字元,其method的使用方法如下:
char charAt(int index);
舉例來說,在程式中宣告二個變數,分別是接收由鍵盤讀入的整列字串的字串變數 str,以及存放字元的變數 ch:
String str;//接收由鍵盤讀入的整列字串
char ch;//存放字串 str 的第 0 個字元。
當我們從鍵盤讀取資料後,即可利用 charAt() method 來取得資料字串中的字元,Java 字串的字元位置編號由 0 開始,因此可以寫出如下的敘述:
ch=str.charAt(0);//取得字串 str 的第 0 個位置的字元
接下來,請將鍵盤中輸入的字元,以前後3個*包圍,假設輸入的字元為A,則輸出結果為***A***。
/* 本題部分程式碼如下 */
01// hw2 ,輸入字元
02importjava.io.*;
03public class hw2
04{
05public static void main(String args[]) throws IOException
06{
07BufferedReader buf;
08String str;
09char ch;
10
11buf=new BufferedReader(new InputStreamReader(System.in));
12System.out.print(“請輸入一個字元:”);
13str=buf.readLine();
14ch=str.charAt(0);
15// 請在此補足此程式所需的程式碼,完成本題要求
16}
17}
第一題的意思應該是求小於1000的完美數~
還有第一題的程式碼有詳細的解說嗎?
因為現在這樣看我有點不太懂><"
第二題我編譯 結果它出現這樣耶
--------------------Configuration: --------------------
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
-client to select the "client" VM
-server to select the "server" VM
-hotspot is a synonym for the "client" VM [deprecated]
The default VM is client.
-cp
-classpath
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D =
set a system property
-verbose[:class|gc|jni]
enable verbose output
-version print product version and exit
-version:
require the specified version to run
-showversion print product version and continue
-jre-restrict-search | -jre-no-restrict-search
include/exclude user private JREs in the version search
-? -help print this help message
-X print help on non-standard options
-ea[: ...|: ]
-enableassertions[: ...|: ]
enable assertions
-da[: ...|: ]
-disableassertions[: ...|: ]
disable assertions
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-agentlib: [= ]
load native agent library , e.g. -agentlib:hprof
see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath: [= ]
load native agent library by full pathname
-javaagent: [= ]
load Java programming language agent, see java.lang.instrument
-splash:
show splash screen with specified image
Process completed.
這是怎麼一回事@@" 是因為我JCreater不是專業版嗎?
第一題可以跑出來 也弄懂了^^
問題似乎解決了!!!
謝謝兩位大大^^
2 Answers
- 胤煌Lv 51 decade agoFavorite Answer
第一題:
public class perfectNumber
{
public static void main(String[]argv)
{
String tmp = "";
for(int i = 1;i <= 1000;i++)
{
tmp = perfect(i);
if(!tmp.equals("false"))//是完美數才輸出
System.out.println(tmp);
}
}
static String perfect(int i)
{
int check = 0;
for(int j = 1;j <= i/2;j++)//找因數
{
if(i%j == 0)//是因數
check += j;//因數加總
}
if(check == i)//是完美數
return ""+i;
else
return "false";//不是完美數
}
}
第二題:
import java.io.*;
public class hw2
{
public static void main(String args[]) throws IOException
{
BufferedReader buf;
String str;
char ch;
buf=new BufferedReader(new InputStreamReader(System.in));
System.out.print("請輸入一個字元:");
str=buf.readLine();//輸入
ch=str.charAt(0);//抓第一個字元
for(int i = 0;i < 3;i++)//輸出3個 *
System.out.print("*");
System.out.print(ch);//輸出字元
for(int i = 0;i < 3;i++)//輸出3個 *
System.out.print("*");
}
}
2010-02-17 20:12:21 補充:
我用的是JCreator喔
也是免費版的
我的編譯沒有問題
如果是熊熊大大的程式碼出問題的話
那是因為有亂碼才會這樣
你要先把亂碼去除喔
2010-02-17 20:16:42 補充:
恩
我的的確沒問題
你可以用打的試試看
不要用複製的
複製常常會複製到隱藏的字元
- 1 decade ago
第一個問題 什麼叫1000的完美數= = 還是你要問他是不是完美數呢??
1000不是perfectNumberI喔
這是測試方法
public static boolean perfectNum(long n){
long a, b;
for (a = 1, b = 0; a < n; a++) {
if (n % a != 0) {
continue;
}
b+=a;
}
if (b==n){ return true;}
return false;
}
第二個等我~~~
2010-02-17 19:36:39 補充:
System.out.println("***" + str.charAt(0) + "***");
加這一行吧....
2010-02-17 19:37:20 補充:
這是第二題的
2010-02-17 19:54:54 補充:
第一題解說
採循序測試,a從1測試到n-1,判斷a是否是n的因數
若是,則b+=a。
n代表所測試的數字
第一題的main方法
public static void main(String[] args) {
for(int sum=0;sum<1000;sum++){
if(perfectNum(sum)){
System.out.println(sum+"是完美數");
}
}
p.s 第一題的for loop 是a
2010-02-17 19:55:30 補充:
a<n
2010-02-17 19:59:55 補充:
及時:a894612005不會再盡力幫你解答喔 我也在學^^
2010-02-17 20:07:31 補充:
你用另外一個人的??
還是我的??
我是用netbeans耶
2010-02-17 20:11:44 補充:
你用打的看看不要用複製的 不然常常會出問題
我用複製會產生 一力GO 的錯誤