在我的代码中,我需要使用if语句,具体取决于用户输入的内容以绘制一定数量的pawn。因此,如果用户输入4个棋子,我需要其中三个处于随机位置。所以我需要生成随机x和y在我的画布大小之间协调。我知道我需要使用随机数生成器,但我不确定它是如何工作的。请帮帮我。谢谢 这是我的代码......
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class ChessSet extends JPanel
{
public int numPawns;
public int numBishops;
public static void main()
{
JFrame ourFrame = new JFrame();
ourFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
ourFrame.setSize(1000,1000);
ChessSet ourChessSet = new ChessSet();
Scanner key = new Scanner( System.in );
System.out.println( "How many pawns would you like? (0-4): " );
ourChessSet.numPawns = key.nextInt();
System.out.println( "How many bishops would you like? (0-4): " );
ourChessSet.numBishops = key.nextInt();
ourFrame.add( ourChessSet );
ourFrame.setBackground( Color.white );
ourFrame.setVisible(true);
}
public void paint( Graphics canvas )
{
ChessSet inst1 = new ChessSet();
this.drawPawn( canvas, 100, 140, Color.black );
this.drawKing( canvas, 375, 50, Color.black );
this.drawRook( canvas, 600, 110, Color.black );
this.drawQueen( canvas, 100, 535, Color.black );
this.drawKnight( canvas, 350,480, Color.black );
this.drawBishop( canvas, 700, 480, Color.black );
canvas.drawString( "King 1", 10, 10 );
canvas.drawString( "Rook 1", 10, 30 );
canvas.drawString( "Queen 1", 10, 20 );
canvas.drawString( "Pawn " + numPawns, 100, 10 );
canvas.drawString( "Bishop " + numBishops, 100, 30 );
canvas.drawString( "Knight 1", 100, 20 );
if( numPawns == 1 )
{
this.drawPawn( canvas, 100, 140, Color.black );
}
else if( numPawns == 2 )
{
this.drawPawn( canvas, 100, 140, Color.black );
this.drawPawn( canvas, 800, 800, Color.black );
}
else if( numPawns == 3 )
{
}
else if( numPawns == 4 )
{
}
else
{
}
if( numBishops == 1 )
{
this.drawBishop( canvas, 100, 140, Color.cyan );
}
else if( numPawns == 2 )
{
this.drawBishop( canvas, 100, 140, Color.black );
}
else if( numBishops == 3 )
{
}
else if( numBishops == 4 )
{
}
else
{
}
}
public void drawPawn( Graphics canvas, int x, int y, Color pawnColor )
{
canvas.setColor( pawnColor );
canvas.fillOval( x-10, y-45, 120, 120 );//top oval
canvas.fillRect( x+0, y+50, 100, 150 );//middle rect
canvas.fillRoundRect( x-35, y+200, 170, 70, 10, 10 );//base rect
canvas.setColor( Color.white );
canvas.fillOval( x-25 , y+75, 50, 125 );//oval out of middle
canvas.fillOval( x+75, y+75, 50, 125 );//oval out of middle
}
public void drawKing( Graphics canvas, int x, int y, Color kingColor )
{
canvas.setColor( kingColor );
canvas.setColor( Color.black );
canvas.fillRect( x-60, y+180, 210, 120 );//seconds rect from the bottom
canvas.setColor( Color.white );
canvas.fillOval( x-90, y+165, 60, 120 );//ovals out of second from bot
canvas.fillOval( x+120, y+165, 60, 120 );//ovals out of second from bot
canvas.setColor( Color.black );
canvas.fillRoundRect( x-90, y+300, 270, 60, 10, 10 );//base rect
canvas.fillRect( x+30, y-0 , 30, 90 );//cross rect
canvas.fillRect( x+0, y+30, 90, 30 );//cross rect
canvas.fillRect( x-15, y+90, 120, 60 );//first rect from top
canvas.fillRect( x-15, y+150, 120, 30 );//first rect from top
canvas.setColor( Color.white );
canvas.fillOval( x-30, y+30, 30, 120 );//oval out of top rect
canvas.fillOval( x+90, y+30, 30, 120 );//oval out of top rect
canvas.setColor( Color.black );
}
public void drawRook( Graphics canvas, int x, int y, Color rookColor )
{
canvas.setColor( rookColor );
canvas.setColor( Color.black );
canvas.fillRect( x+15, y+90, 150, 180 );//middle rect
canvas.setColor( Color.white );
canvas.fillOval( x-30, y+30, 60, 240);//ovals on side of bottom rect
canvas.fillOval( x+150, y+30, 60, 240);//ovals on bottom of rect
canvas.setColor( Color.black );
canvas.fillRoundRect( x+0, y+0, 180, 90, 20, 20);//top rec
canvas.fillRoundRect( x+0, y+240, 180, 60, 10, 10);//base rectangle
canvas.setColor( Color.white );
canvas.fillRect( x+40, y+0, 15, 30);//next 3 lines are slits in top
canvas.fillRect( x+80, y+0, 15, 30);
canvas.fillRect( x+120, y+0, 15, 30);
}
public void drawQueen( Graphics canvas, int x, int y, Color queenColor )
{
canvas.setColor( queenColor );
canvas.setColor( Color.black );
canvas.fillRect( x+0, y+0, 150, 240 ); //main body rectangle
canvas.fillOval(x-30, y+180, 60, 60);//circles added to base
canvas.fillOval(x+120, y+180, 60, 60);//circles added to base
canvas.fillOval(x+55, y-45, 40, 40);//circle on top
canvas.setColor( Color.white );
canvas.fillOval( x-45, y-30, 90, 210);//ovals out of middle rect
canvas.fillOval( x+105, y-30, 90, 210);//ovals out of middle rect
canvas.setColor( Color.black );
canvas.fillRoundRect( x+30, y+60, 90, 30, 10, 10 );//rect in middle of body
}
public void drawKnight( Graphics canvas, int x, int y, Color knightColor )
{
canvas.setColor( Color.black );
Polygon tri = new Polygon();
tri.addPoint( x+0, y+75 );
tri.addPoint( x+210, y+75 );
tri.addPoint( x+105, y+165 );
canvas.fillPolygon( tri );
canvas.fillRoundRect( x-0, y+240, 210, 60, 10, 10 );//base rectangle
canvas.fillOval( x+30, y+130, 60, 120 );//left oval
canvas.fillOval( x+120, y+130, 60, 120 );//right oval
canvas.setColor( Color.white );
canvas.fillOval( x+100, y+105, 15, 15 );
canvas.fillOval( x+100, y+135, 15, 15 );
}
public void drawBishop( Graphics canvas, int x, int y, Color bishopColor )
{
canvas.setColor( Color.black );
canvas.fillOval( x+0, y+0, 90, 90 );//head
canvas.fillRoundRect( x-30, y+90, 150, 30, 15, 15 );//shoulders
canvas.fillRect( x-30, y+120, 150, 120 );//body
canvas.fillOval( x+30, y-30, 30, 30 );
canvas.setColor( Color.white );
canvas.fillOval( x-75, y+120 , 90, 120 );//ovals out of body
canvas.fillOval( x+75, y+120 , 90, 120 );//ovals out of body
canvas.fillRect( x+20, y+0, 15, 50 );//slit out of head
canvas.setColor( Color.black );
canvas.fillRoundRect( x-60, y+240, 210, 60, 10, 10 );//base rect
}
}
答案 0 :(得分:2)
Uri uriSMS = Uri.parse("content://sms/sent");
Cursor cur = getActivity().getContentResolver().query(uriSMS , null, null, null, null);
Cursor messagesCursor = getActivity().getContentResolver().query(uriSMS , new String[]{"_id", "address", "body", "person", "date",}, null, null, null);
if(messagesCursor.getCount() > 0) {
try {
while (messagesCursor.moveToNext())
{
int x=messagesCursor.getInt(messagesCursor.getColumnIndex("_id"));
String address=messagesCursor.getString(messagesCursor.getColumnIndex("address"));
String body=messagesCursor.getString(messagesCursor.getColumnIndex("body"));
String person=messagesCursor.getString(messagesCursor.getColumnIndex("person"));
String date_=messagesCursor.getString(messagesCursor.getColumnIndex("date"));
}
}
catch(Exception ex)
{
}
}
更多详细信息,请参阅此链接See
答案 1 :(得分:1)
提示:
<Extensions>
<Extension Category="windows.publisherCacheFolders">
<PublisherCacheFolders>
<Folder Name="test" />
</PublisherCacheFolders>
</Extension>
</Extensions>
将返回0到999之间的随机值
如果我想要1到1000之间的值,我会做
Random random = new Random();
int limit = 1000;
int randomValue = random.nextInt(limit);
答案 2 :(得分:1)
如果您需要生成从最小到最大(包括两者)的数字,请编写
var result = customer.SelectMany(x => x.orders);
var Least = result.Distinct().OrderBy(x => x.price).Take(3);
根据documentation,此方法调用返回&#34;伪随机,均匀分布的int值介于0(包括)和指定值(不包括)&#34;之间。