我正在使用这些Scanner方法nextInt()并nextLine()阅读输入.
它看起来像这样:
System.out.println("Enter numerical value");
int option;
option = input.nextInt(); // Read numerical value from input
System.out.println("Enter 1st string");
String string1 = input.nextLine(); // Read 1st string (this is skipped)
System.out.println("Enter 2nd string");
String string2 = input.nextLine(); // Read 2nd string (this appears right after reading numerical value)
Run Code Online (Sandbox Code Playgroud)
问题是输入数值后,第一个input.nextLine()被跳过而第二个input.nextLine()被执行,所以我的输出如下所示:
Enter numerical value
3 // This is my input
Enter 1st string // The program is supposed to stop here and …Run Code Online (Sandbox Code Playgroud) 我开始学习Java,我无法理解"Thinking in Java"一书中的一个例子.在这个例子中,作者表示,他说"简单使用'this'关键字":
//Leaf.java
//simple use of the "this" keyword
public class Leaf {
int i = 0;
Leaf increment() {
i++;
return this;
}
void print() {
System.out.println("i = " + i);
}
public static void main(String[] args) {
Leaf x = new Leaf();
x.increment().increment().increment().print();
}
}
Run Code Online (Sandbox Code Playgroud)
当上面的代码确实正常工作时,我无法理解increment()返回的方法.
它不是变量i,它不是对象x?我只是不明白.我试图修改程序,了解它(如更换return this用return i或print x代替i),但是编译器显示我的错误.
我知道如何使用逗号printf作为分组分隔符以格式打印值
1,000,000.00
打印它我正在使用命令
System.out.printf ("%,.2f", value);
但如何使用空格作为分组分隔符来格式化值
1 000 000.00
我试图找到解决方案,但DecimalFormat现在使用的解决方案让我复杂化(初学者级别).
有逗号这样的简单方法吗?
如何“扩展”JUnit 5与mockito 3?
在JUnit 5使用mockito的小修道院版本3.0测试类需要用一个新的来注解JUnit 5注释@ExtendWith(MockitoExtension.class),例如:
@ExtendWith(MockitoExtension.class)
public class TestClass {
@Mock
DependencyA dependancyA;
public void myTest() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
正因为如此,没有必要使用任何更多MockitoRule与@Rule注释。
我今天试过测试版mockito-android 3.0.0-beta1,但它没有MockitoExtension类。
我应该用什么代替?我找不到 3.0 版的任何文档,这是可以理解的,因为它仍处于测试阶段。
我从书籍"Java.如何编程"中学习Java P.&H.Deitel在页216中有一个例子用于final其中一个变量
private static final Random randomNumbers = new Random();
Run Code Online (Sandbox Code Playgroud)
据我所知,变量均值中的声明最终,该变量是一种常量,这意味着当初始化时你不能再改变它.但上面的对象(变量)在程序中使用两次以返回随机数
int die1 = 1 + randomNumbers.nextInt( 6 );
int die2 = 1 + randomNumbers.nextInt( 6 );
Run Code Online (Sandbox Code Playgroud)
它返回2个不同的(随机)值.我想我在这里丢了东西.程序运行良好,但我不明白final在对象声明中使用的目的是什么?
我有一个带有静态方法的单例类 getNext()
public class Sequence {
private static Sequence instance;
private static int counter;
private Sequence () { // note: private constructor
counter = 0;
}
public static Sequence getInstance(){
if(instance==null) { // Lazy instantiation
instance = new Sequence();
}
return instance;
}
public static int getNext(){ return ++counter;}
}
Run Code Online (Sandbox Code Playgroud)
在我的测试代码中,我有一个for循环,它调用getNext()了几次
public class TestSequence {
public static void main(String[] args) {
for (int i = 0; i < 5; i++)
System.out.println(Sequence.getNext());
}
Run Code Online (Sandbox Code Playgroud)
}
它的输出是
1
2
3
4 …Run Code Online (Sandbox Code Playgroud) 我有一个物体地图 Members
public class Member {
int id;
String firstName;
String lastName;
String street;
String city;
...
Run Code Online (Sandbox Code Playgroud)
与id作为重点,这是我做的工作,我不想更改地图的结构.
我需要按名称功能实现搜索.到目前为止,我只能想到两个解决方案:
第一是迭代地图 - 不是最节省时间的方式
第二个是创建另一个地图,其成员的名称作为键,ids作为值作为我的主地图的参考,并通过此地图搜索以找到关键 - 有效时间但空间较少,这不会让我太担心
我想问一下,在我的案例中是否有更有效(更好)的方法来实现地图搜索?
有接口
interface Animal extends Comparable<Animal> {
}
Run Code Online (Sandbox Code Playgroud)
和2个班级
class Dog implements Animal {
}
Run Code Online (Sandbox Code Playgroud)
和
class Cat implements Animal {
}
Run Code Online (Sandbox Code Playgroud)
compareTo(Animal o)当争论不是同一个具体实施时应该返回什么Animal?
应该扔IllegalArgumentException吗?
例如,如果我将Dog实例传递给Cat.compareTo().我无法比较它们,因为它们是不同的类型.我不能称super.compareTo()他们的超级是Object没有实现的类型Comparable.铸造Dog到Cat会抛出ClassCastException.
java ×8
collections ×1
comparable ×1
dictionary ×1
interface ×1
io ×1
junit5 ×1
mockito ×1
singleton ×1