
正文
大神运用java写代码 写java代码用什么软件好
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
求java大神帮忙写个代码,判断一个整数是否是回文数字,麻烦加下注释
public static void main(String[] args) {
int c = 475898574;
String a = c+""; //将整数变成字符串
String b = new StringBuffer(a).reverse().toString(); //用reverse(),将字符串反转
if (a.equals(b)) { //对比
System.out.println("是回文数");
}else{
System.out.println("不是回文数");
}
}
望采纳!
相关问答
Q1: 求大神用Java编写这个程序,要有具体代码,万分感谢,定采纳
我没加注释,你有什么就直接问吧。
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Cource {
private int cNumber;
private String cName;
private int cUnit;
public Cource(int cNumber, String cName, int cUnit) {
super();
this.cNumber = cNumber;
this.cName = cName;
this.cUnit = cUnit;
}
public int getcNumber() {
return cNumber;
}
public void setcNumber(int cNumber) {
this.cNumber = cNumber;
}
public String getcName() {
return cName;
}
public void setcName(String cName) {
this.cName = cName;
}
public int getcUnit() {
return cUnit;
}
public void setcUnit(int cUnit) {
this.cUnit = cUnit;
}
public void printCourceInfo(){
try {
File file=new File("out.txt");
if (!file.exists()) {
file.createNewFile();
}
BufferedWriter a=new BufferedWriter(new FileWriter(file));
a.write("课程编号,课程名,学分数");
a.newLine();
a.write(this.cNumber+","+this.cName+","+this.cUnit);
a.close();
System.out.println("输出成功");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import test.Cource;
public class Test {
public static void main(String[] args) {
Cource test=new Cource(121227, "课程名为Java程序设计", 3);
test.printCourceInfo();
}
}
Q2: 寻求大神帮忙写Java代码,要用Dijkstra’s algorithm(迪杰斯特拉算法)
package minRoad.no;
import java.util.Arrays;
//这个程序用来求得一个图的最短路径矩阵
public class ShortestDistance_V4 {
private static final int inf = Integer.MAX_VALUE;// 表示两个点之间无法直接连通
public static int[][] dijkstra(int[][] graph) {
int min, v, u = 0, n = graph.length;
int[] path = new int[n];
int[] dist = new int[n];
boolean[] s = new boolean[n];
Arrays.fill(s, false);
Arrays.fill(dist, inf);
for (int i = 0; i n; i++) {
dist[i] = graph[u][i];
if (i != u dist[i] inf)
path[i] = u;
else
path[i] = -1;
}
s[u] = true;
while (true) {
min = inf;
v = -1;
// 找到最小的dist
for (int i = 0; i n; i++) {
if (!s[i]) {
if (dist[i] min) {
min = dist[i];
v = i;
}
}
}
if (v == -1) break;// 找不到更短的路径了
// 更新最短路径
s[v] = true;
for (int i = 0; i n; i++) {
if (!s[i] graph[v][i] != inf dist[v] + graph[v][i] dist[i]) {
dist[i] = dist[v] + graph[v][i];
path[i] = v;
}
}
}
// 输出路径
int[] shortest = new int[n];
for (int i = 1; i n; i++) {
Arrays.fill(shortest, 0);
int k = 0;
shortest[k] = i;
while (path[shortest[k]] != 0) {
k++;
shortest[k] = path[shortest[k - 1]];
}
k++;
shortest[k] = 0;
}
int[] tmp = new int[shortest.length];
for (int i = 0; i tmp.length; i++) {
tmp[i] = shortest[tmp.length - i - 1];
}
return new int[][] { dist, tmp };
}
/**
* pre
* v0
* 1, v1
* 4, 2, v2
* inf, 7, -1, v3
* inf, 5, 1, 3, v4
* inf, inf, inf, 2, 6, v5
* /pre
*
* *
*
* pre
* A--------30-------D
* |\ ∧|
* | \ / |
* | \ / |
* | 10 10 |
* | \ / 20
* | \ / |
* | \ / |
* | ∨ / ∨
* 20 B E
* | / ∧
* | / /
* | / /
* | 5 /
* | / 30
* | / /
* | / /
* ∨∠ /
* C
* /pre
*
* @param args
*/
public static void main(String[] args) {
int[][] W1 = {
{ 0, 10, 20, 30, inf },
{ 10, 0, 5, 10, inf },
{ 20, 5, 0, inf, 30 },
{ 30, 10, inf, 0, 20 },
{ inf, inf, 30, 20, 0 },
};
//
//
// int[][] W = {
// { 0, 1, 4, inf, inf, inf },
// { 1, 0, 2, 7, 5, inf },
// { 4, 2, 0, inf, 1, inf },
// { inf, 7, inf, 0, 3, 2 },
// { inf, 5, 1, 3, 0, 6 },
// { inf, inf, inf, 2, 6, 0 }};
int[][] distAndShort = dijkstra(W1);
System.out.println(Arrays.toString(distAndShort[0]));
System.out.println(Arrays.toString(distAndShort[1]));
// distance: { 0, 1, 3, 7, 4, 9};
}
}
关于大神运用java写代码和写java代码用什么软件好的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






