从for循环找到最大值 [英] Finding the maximum value from a for loop
问题描述
我创建的代码在寻找回文数字时给了我成千上万的解决方案。任务是找到可能的最高数字:
pre $ public static void main(String [] args){
long产品; (int j = 999; j> = 100; j--){
;
outer:for(int i = 999; i> = 100; i--) product = j * i;
if(reverse(product)){
System.out.printf(%d *%d =%d%n,i,j,product);
继续;
$ b private static final boolean reverse(long value){
String str = String.valueOf(value);
return str.equals(new StringBuilder(str).reverse()。toString());
}}
我怎样才能让代码只显示最高的值for循环创建?
解决方案描述在这个答案在代码:
long highest_palindrome = 0L;
int storeI = 0;
int storeJ = 0;
长的产品; (int j = 999; j> = 100; j--){
;
outer:for(int i = 999; i> = 100; i--) product = j * i;
if(product< highest_palindrome)break;
if(reverse(product)){
if(product> highest_palindrome){
highest_palindrome = product;
storeI = i;
storeJ = j;
}
继续;
$ b System.out.printf(%d *%d =%d%n,storeI,storeJ,highest_palindrome);
I have created code that gives me thousands of solutions when looking for palindromic numbers. The task is to find the highest number possible:
public static void main(String[] args) {
long product;
outer: for (int i = 999; i >= 100; i--) {
for (int j = 999; j >= 100; j--) {
product = j * i;
if (reverse(product)) {
System.out.printf("%d * %d = %d%n", i, j, product);
continue;
}
}
}
}
private static final boolean reverse(long value) {
String str = String.valueOf(value);
return str.equals(new StringBuilder(str).reverse().toString());
}}
How can I make it so that the code only displays the highest value that the for loop creates?
The solution decribed in this answer in code:
long highest_palindrome = 0L;
int storeI = 0;
int storeJ = 0;
long product;
outer: for (int i = 999; i >= 100; i--) {
for (int j = 999; j >= 100; j--) {
product = j * i;
if (product < highest_palindrome) break;
if (reverse(product)) {
if (product > highest_palindrome) {
highest_palindrome = product;
storeI = i;
storeJ = j;
}
continue;
}
}
}
System.out.printf("%d * %d = %d%n", storeI, storeJ, highest_palindrome);
这篇关于从for循环找到最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!