導航:首頁 > 編程語言 > java編寫水仙花

java編寫水仙花

發布時間:2022-10-03 03:32:32

❶ 用java編程求出水仙花數將它們寫入文件file.txt中,然後從文件中讀出,在屏幕上輸出顯示

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;

public class Number
{
public static void main(String[] args) throws Exception
{
//文件放在d盤,名字為file.txt
File f = new File("d:"+File.separator+"file.txt");
f.createNewFile();
writerFile(f, 1000000);//數值自己輸入!不過太大了可能要等好久
printConsole(f);
}

/**
* 讀取文件列印的控制台
*
* @param f
* @throws Exception
*/
private static void printConsole(File f) throws Exception
{
BufferedReader bReader = new BufferedReader(new FileReader(f));
String str = null;
while ((str = bReader.readLine()) != null)
{
System.out.println(str);
}
bReader.close();
}

/**
* 獲取水仙花數寫入文件
*
* @param f
* @param max
* @throws Exception
*/
private static void writerFile(File f, int max) throws Exception
{
PrintWriter out = new PrintWriter(f);
for (int i = 100; i < max; i++)
{
String iString = i + "";
char[] ic = iString.toCharArray();
double sum = 0;
for (int j = 0; j < ic.length; j++)
{
sum = sum + Math.pow(new Double((ic[j] - '0')), new Double(ic.length));
}
if (sum == i)
{
out.println(iString);
}
}
out.close();
}
}

❷ java編的水仙花數

你從給i賦值的時候就錯了.一個3位數對100取余後,結果是他的後兩位數.就是說如果這個3位數是123的話,123%100的結果是23而不是你想要求的1.所以給i,j,k賦值的幾句代碼可以改為:i=num/100;j=num/10%10;k=num%10;這樣就沒有錯誤了.

❸ java水仙花用combine寫

水仙花數是指一個 n 位數 ( n≥3 )。
水仙花數是指一個 n 位數 ( n≥3 ),它的每個位上的數字的 n 次冪之和等於它本身。(例如:1^3 + 5^3+ 3^3 = 153)。

❹ java編程求水仙花數

一個是0到999循環帶入
一個是多層循環
區別應該在代碼性能上
可以將數值加大 然後使用程序運行時間進行對比一下

第2個 abc都為0的時候 也能成立啊,所以你應該 修改成從1開始初始化abc

❺ java求水仙花數

按照你的要求編寫的求N位水仙花數的程序如下

importjava.util.Scanner;
publicclassCCC{
publicstaticvoidmain(String[]args){
System.out.print("請輸入正整數N(3<=N<=7):");
Scannersc=newScanner(System.in);
intN=sc.nextInt();
if(N<3||N>7){
System.out.println("N<3或者N>7");
return;
}
intmin=(int)Math.pow(10,N-1);
intmax=(int)Math.pow(10,N);
for(inti=min;i<max;i++){
inttmp=i;
intj=0;
inta[]=newint[N];
while(tmp!=0){
a[j]=tmp%10;
tmp=tmp/10;
j++;
}
intsum=0;
for(intk=0;k<N;k++){
sum=sum+(int)Math.pow(a[k],N);
}
if(sum==i){
System.out.println(i);
}
}
}
}

運行結果

請輸入正整數N(3<=N<=7):4

1634

8208

9474

❻ 水仙花數 java 程序

列印出所有的"水仙花數"的程序:
package shuixianhuashu;
import shuixianhuashu.shuixianhuashu; public class shuixianhuashu {
static Boolean isSxh(int m){ int a1,a2,a3; a1=m/100; a2=m%100/10; a3=m%10; if(m<3) return false;
else if( (a1*a1*a1+a2*a2*a2+a3*a3*a3) == (a1*100+a2*10+a3) )
return true; else
return false;
}
public static void main(String[] args){ System.out.println("1000以內的水仙花數:"); shuixianhuashu s = new shuixianhuashu(); for(int i=101;i<1000;i++) if(s.isSxh(i))
System.out.print(i+"\t"); } }

運行結果:
1000以內的水仙花數: 153
370 371 407
【擴展】
所謂"水仙花數"是指一個三位數,其各位數字 立方和等於該數本身。

❼ 怎樣用Java編寫「水仙花」

水仙花數是指一個n位數(n≥3),它的每個位上的數字的n次冪之和等於它本身。(例如:1^3+5^3+3^3=153)

❽ JAVA編水仙花數

public void isNumber(int num)
{
String numm=""+ num;//首先把你輸入的整型數轉換成字元類型
int len=numm.length();//得到你輸入的數的個數(你要判斷的數是三位數、四位數、、、)
int [] nums=new int [len];//聲明一個數組來保存輸入的這個數分別拆分後的單個值,如153 (1、5、3)
int temp=0,i=0;
while(num>10)//用循環分別把這個位的每個位上的值保存在數組中
{
nums[i] = num % 10;
String nu=numm.substring(0, (numm.length()-(i+1)));
num =Integer.parseInt(nu);

i++;
if(num<10)
{
nums[i]=num;
break;
}
}
for (int j = 0; j < nums.length;j++)
{//用循環得到數組中無素的值累加
temp += (int) Math.pow(nums[j], nums.length);
}
num=Integer.parseInt(numm);
if(temp==num)//判斷累加後的結果是否與輸入的值一致
{
( System.err.println(num+"是水仙花數!"); }
else
{
System.err.println(num+"不是水仙花數!"); }
}

public static void main(String [] args)
{
Scanner input =new Scanner(System.in);
System.out.println("請輸入一個數字:");
int num = input.nextInt();
isNumber(num);
}
}

❾ java編程學習水仙花數問題

你的程序主要是每個數前,sum變數沒有置0

改正後的水仙花數的Java程序如下(改動的地方見注釋)

public class a99{

public static void main(String[] arge){

int sum=0,t=0;

for(int i=100;i<1000;i++){

t=i;

sum=0;//這里加一句,每個數字都要初始化sum=0

while(t!=0){

sum += ((t%10)*(t%10)*(t%10));

t /= 10;

}

if(sum==i)

System.out.println(i+"是水仙花數");//這里printf改成println

}

}//這里加大括弧

}//這里加大括弧

❿ 用Java寫個關於「水仙花數」的程序

按一下代碼執行:

public class woo {

public static void main(String args[]) {

System.out.println("100-1000中的水仙花數有:");

for(int i=100;i<1000;i++){

int single = i%10;

int ten = i/10%10;

int hundred = i/10/10%10;

//水仙花數判斷要求

if(i == (single*single*single+ten*ten*ten+hundred*hundred*hundred)){

System.out.println(i);

}

}

}

}

(10)java編寫水仙花擴展閱讀

水仙花數只是自冪數的一種,嚴格來說3位數的3次冪數才稱為水仙花數。

一位自冪數:獨身數

兩位自冪數:沒有

三位自冪數:水仙花數

四位自冪數:四葉玫瑰數

五位自冪數:五角星數

六位自冪數:六合數

七位自冪數:北斗七星數

八位自冪數:八仙數

九位自冪數:九九重陽數

十位自冪數:十全十美數

閱讀全文

與java編寫水仙花相關的資料

熱點內容
protues單片機 瀏覽:679
淘寶想多開店怎麼租伺服器 瀏覽:580
小鹿情感app怎麼打不開了 瀏覽:325
可自編譯的C 瀏覽:62
vfl90壓縮機是哪個廠家 瀏覽:677
安卓系統游戲怎麼開發 瀏覽:410
抖助力app綁定的銀行卡怎麼辦 瀏覽:466
我的電腦文件夾打開方式 瀏覽:931
東莞加密u盤公司 瀏覽:137
graphvizlinux 瀏覽:438
智能手錶藍牙怎麼下載app 瀏覽:293
女程序員下班菜譜 瀏覽:260
加密貨幣買什麼比較靠譜 瀏覽:277
用圖片的地圖再編譯的地方 瀏覽:462
python監控系統進程 瀏覽:236
群暉怎麼取消照片共享文件夾 瀏覽:156
程序員那麼可愛第幾集陸璃懷孕 瀏覽:615
西門子st編程手冊 瀏覽:59
mt4編程書籍 瀏覽:21
單片機模擬實驗設置電壓 瀏覽:948