变量值是“从未使用过”的java
这可能只是一个noob问题,但我发现很难摆脱这种心理障碍 其中我有:变量值是“从未使用过”的java,java,oop,intellij-idea,Java,Oop,Intellij Idea,这可能只是一个noob问题,但我发现很难摆脱这种心理障碍 其中我有: if (nrOfDig % 3 == 0) nrOfFive = nrOfDig; else if(nrOfDig % 5 == 0) nrOfThree = nrOfDig; 应分配稍后在此过程中使用的值: while (nrOfFive > 0) { result = result + 5 * value; value = value * 10; nrOfFive--; }
if (nrOfDig % 3 == 0)
nrOfFive = nrOfDig;
else if(nrOfDig % 5 == 0)
nrOfThree = nrOfDig;
应分配稍后在此过程中使用的值:
while (nrOfFive > 0) {
result = result + 5 * value;
value = value * 10;
nrOfFive--;
}
我不明白为什么这些值分配不正确。我做错了什么
完整代码
为变量赋值后,该变量未被使用。您没有使用它执行其他计算或返回它 分享你的代码。现在value似乎还没有值,但是如果没有剩下的代码,我就说不出来。请在问题中包含您的代码,而不是作为链接,由于此链接可能一直无效,代码将消失。@Mr.polywhill如果我没有完全关闭,这应该是一个没有任何堆栈跟踪的简单编译器警告。我没有使用的变量是nrOfFive**i wasmistaking@KevinEsche我想我确实做错了什么,因为它没有按应有的方式工作:。。。我对警告不感兴趣,但就其含义而言,错误警告实际上明确地说变量“value”从未使用过——nrOfFive不是value
package com.company;
import java.util.Scanner;
/**
* Sherlock Holmes suspects his archenemy, Professor Moriarty, is once again
* plotting something diabolical. Sherlock's companion, Dr. Watson, suggests
* Moriarty may be responsible for MI6's recent issues with their
* supercomputer, The Beast.
* Shortly after resolving to investigate, Sherlock receives a note from
* Moriarty boasting about infecting The Beast with a virus; however, he also
* gives him a clue—a number, NN. Sherlock determines the key to removing the
* virus is to find the largest Decent Number having NN digits.
*
* A Decent Number has the following properties:
* - Its digits can only be 3's and/or 5's.
* - The number of 3's it contains is divisible by 5.
* - The number of 5's it contains is divisible by 3.
* - If there are more than one such number, we pick the largest one.
* Moriarty's virus shows a clock counting down to The Beast's destruction, and
* time is running out fast. Your task is to help Sherlock find the key before
* The Beast is destroyed!
*
* Constraints
* - 1≤T≤201≤T≤20
* - 1≤N≤1000001≤N≤100000
*
* Input Format
* - The first line is an integer, TT, denoting the number of test cases.
* - The TT subsequent lines each contain an integer, NN, detailing the number
* of digits in the number.
*
* Output Format
* - Print the largest Decent Number having NN digits; if no such number
* exists, tell Sherlock by printing -1.
*
* Sample Input
* >> 4
* >> 1
* >> 3
* >> 5
* >> 11
*
* Sample Output
* >> -1
* >> 555
* >> 33333
* >> 55555533333
*
* Explanation
* - For N=1N=1, there is no decent number having 11 digit (so we print −1−1).
* - For N=3N=3, 555555 is the only possible number. The number 55 appears
* three times in this number, so our count of 55's is evenly divisible by 33
* (Decent Number Property 3).
* - For N=5N=5, 3333333333 is the only possible number. The number 33 appears
* five times in this number, so our count of 33's is evenly divisible by 55
* (Decent Number Property 2).
* - For N=11N=11, 55555533333 and all permutations of these digits are valid
* numbers; among them, the given number is the largest one.
*/
public class Solution {
public static void main(String[] args) {
int testCases, nrOfDig, result = -1;
Scanner in = new Scanner(System.in);
testCases = in.nextInt();
while (testCases > 0) {
nrOfDig = in.nextInt();
result = Solution.result(nrOfDig);
System.out.println(result);
testCases--;
}
}
private static int result(int nrOfDig) {
int result = 0;
int nrOfFive = 0, nrOfThree = 0, temp, value = 1;
// Find out how many digits fit in the number.
if (nrOfDig % 3 == 0)
nrOfFive = nrOfDig;
else if (nrOfDig % 5 == 0)
nrOfThree = nrOfDig;
else {
temp = nrOfDig % 3;
if (temp % 5 == 0) {
nrOfFive = temp * 3;
nrOfThree = temp % 5 * 5;
} else {
temp = nrOfDig % 5;
if (temp % 3 == 0) {
nrOfThree = temp * 5;
nrOfFive = temp % 3 * 3;
} else
return -1;
}
// End digitNumberFind algortithm
// Start Number Making
while (nrOfFive > 0) {
result = result + 5 * value;
value = value * 10;
nrOfFive--;
}
value = 1;
while (nrOfThree > 0) {
result = result * 10 + 3 * value;
value = value * 10;
nrOfThree--;
}
return result;
}
return result;
}
}