我确定这将是一个简单的问题,但我似乎找不到正确的答案。我有两个不同的String数组,每个数组都有数据,并且每个数组的长度都完全相同(21)。我只是试图遍历两个索引,并在同一行上打印数组1的索引值0和数组2的索引值0,依此类推。
这就是我所拥有的。我有一个名为weightToOunce的数组,另一个名为weightArray。该代码的问题在于它基于两个数组的长度,因此数组中的每个索引值都将以数组的长度打印(在这种情况下为21次)。
我尝试将print语句移出循环,并在循环之前声明一个int i和int j变量,但是我也没有成功。
public static void weightArrayLooper() {
String[] weightToOunce = Project1.setWeightPercentageToOunce();
for (int i = 0;i<weightArray.length;i++) {
for (int j = 0;i<weightToOunce.length;j++) {
System.out.println("Lb " + weightArray[i] + " Oz " + weightToOunce[j]);
}
}
}
答案 0 :(得分:4)
不确定这是否是您要尝试的操作,但是可以使用相同的'i'值遍历两个数组!
#define UART4_PORT GPIOA
#define UART4_TX_PIN LL_GPIO_PIN_0
#define UART4_RX_PIN LL_GPIO_PIN_1
int USART_Setup(USART_TypeDef *USARTx, uint32_t baud_rate)
{
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
if (USARTx == UART4)
{
irq_num = UART4_IRQn;
/* Configure Tx Pin as : Alternate function, High Speed, Push pull, Pull up */
LL_GPIO_SetPinMode(UART4_PORT, UART4_TX_PIN, LL_GPIO_MODE_ALTERNATE);
LL_GPIO_SetAFPin_8_15(UART4_PORT, UART4_TX_PIN, LL_GPIO_AF_8);
LL_GPIO_SetPinSpeed(UART4_PORT, UART4_TX_PIN, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(UART4_PORT, UART4_TX_PIN, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinPull(UART4_PORT, UART4_TX_PIN, LL_GPIO_PULL_UP);
/* Configure Rx Pin as : Alternate function, High Speed, Push pull, Pull up */
LL_GPIO_SetPinMode(UART4_PORT, UART4_RX_PIN, LL_GPIO_MODE_ALTERNATE);
LL_GPIO_SetAFPin_8_15(UART4_PORT, UART4_RX_PIN, LL_GPIO_AF_8);
LL_GPIO_SetPinSpeed(UART4_PORT, UART4_RX_PIN, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(UART4_PORT, UART4_RX_PIN, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinPull(UART4_PORT, UART4_RX_PIN, LL_GPIO_PULL_UP);
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_UART4);
LL_RCC_SetUSARTClockSource(LL_RCC_UART4_CLKSOURCE_PCLK1);
}
NVIC_SetPriority(irq_num, 0);
NVIC_EnableIRQ(irq_num);
/* TX/RX direction */
LL_USART_SetTransferDirection(USARTx, LL_USART_DIRECTION_TX_RX);
/* 8 data bit, 1 start bit, 1 stop bit, no parity */
LL_USART_ConfigCharacter(USARTx, LL_USART_DATAWIDTH_8B, LL_USART_PARITY_NONE, LL_USART_STOPBITS_1);
LL_USART_SetBaudRate(USARTx, SystemCoreClock, LL_USART_OVERSAMPLING_16, baud_rate);
LL_USART_Enable(USARTx);
//enable RX buffer not empty interrupt
USARTx->CR1 |= USART_CR1_RXNEIE;
/* Polling USART initialisation */
while((!(LL_USART_IsActiveFlag_TEACK(USARTx))) || (!(LL_USART_IsActiveFlag_REACK(USARTx))))
{
timeout++;
if (timeout > 1000000)
{
return USART_ERROR;
}
}
return USART_OK;
}