Tag: 交换

使用引用调用交换两个数字

我们可以使用传递引用或通过引用调用在Java中交换两个数字吗? 最近,当我遇到用Java交换两个数字时,我写道 class Swap{ int a,b; void getNos(){ System.out.println(“input nos”); a = scan.nextInt(); b = scan.nextInt(); // where scan is object of scanner class } void swap(){ int temp; temp = this.a; this.a = thisb; this.b = this.a; } } 在main方法中,我调用上面提到的方法并取两个整数a , b然后使用第二种方法我交换两个数字,但相对于对象本身…. 该程序或逻辑是否通过引用传递? 这是正确的解决方案吗?

如何在Java中交换String字符?

如何在String交换两个字符? 例如, “abcde”将变为”bacde” 。

如何“洗牌”arrays?

我正在努力创建一个“shuffleDeck()”方法。 我要做的是创建一个方法,它将采用一个数组参数(这将是卡片组)洗牌,并返回洗牌数组列表。 这是代码: class Card { int value; String suit; String name; public String toString() { return (name + ” of ” + suit); } } public class PickACard { public static void main( String[] args) { Card[] deck = buildDeck(); // display Deck(deck); int chosen = (int)(Math.random()* deck.length); Card picked = deck[chosen]; System.out.println(“You picked […]